First public commit!

This commit is contained in:
Juan Carlos Pujol Mainegra
2017-08-18 03:07:43 -05:00
parent 2c754790e6
commit 5a8ddf6978
91 changed files with 17165 additions and 1 deletions

43
YATC/Scope/Report/Item.cs Normal file
View File

@@ -0,0 +1,43 @@
/*
* Yet Another Tiger Compiler (YATC)
*
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
namespace YATC.ASTNodes
{
public class Item
{
public Item(Level level, int line, int column, string text)
{
Level = level;
Line = line;
Column = column;
Text = text;
}
public readonly Level Level;
public readonly int Line;
public readonly int Column;
public readonly string Text;
}
}

View File

@@ -0,0 +1,79 @@
/*
* Yet Another Tiger Compiler (YATC)
*
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
using System.Collections;
using System.Collections.Generic;
namespace YATC.ASTNodes
{
/// <summary>
/// Indica cuán mal es el error
/// </summary>
public enum Level { Info = 0, Warning = 1, Error = 2 }
public class Report : IEnumerable<Item>
{
protected readonly List<Item> Items = new List<Item>();
public Level Level { get; private set; }
public bool IsOK { get { return Level != Level.Error; } }
public void AddError(int line, int column, string text, params object[] modifiers)
{
if (Level < Level.Error)
Level = Level.Error;
Items.Add(new Item(Level.Error, line, column, string.Format(text, modifiers)));
}
public void AddWarning(int line, int column, string text, params object[] modifiers)
{
if (Level < Level.Warning)
Level = Level.Warning;
Items.Add(new Item(Level.Warning, line, column, string.Format(text, modifiers)));
}
public void AddInfo(int line, int column, string text, params object[] modifiers)
{
if (Level < Level.Info)
Level = Level.Info;
Items.Add(new Item(Level.Info, line, column, string.Format(text, modifiers)));
}
public void Reset()
{
Level = Level.Info;
Items.Clear();
}
public IEnumerator<Item> GetEnumerator()
{
return Items.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}

View File

@@ -0,0 +1,31 @@
/*
* Yet Another Tiger Compiler (YATC)
*
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
namespace YATC.Scope
{
public abstract class FunVarInfo : TigerTypeInfo
{
}
}

View File

@@ -0,0 +1,60 @@
/*
* Yet Another Tiger Compiler (YATC)
*
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
using System.Linq.Expressions;
using System.Reflection;
namespace YATC.Scope
{
public class FunctionInfo : FunVarInfo
{
public FunctionInfo(string name, VariableInfo[] parameterInfo, TigerTypeHolder returnTypeHolder, bool isStandard)
{
this.Name = name;
this.ParameterInfo = parameterInfo;
this.Holder = returnTypeHolder;
this.IsStandard = isStandard;
}
public FunctionInfo(string name, VariableInfo[] parameterInfo, TigerTypeHolder returnTypeHolder,
MethodInfo methodInfo)
:this(name, parameterInfo, returnTypeHolder, true)
{
this.MethodInfo = methodInfo;
}
public MethodInfo MethodInfo;
/// <summary>
/// Listado de parámetros
/// </summary>
public readonly VariableInfo[] ParameterInfo;
/// <summary>
/// Función en la máquina virtual correspondiente
/// </summary>
public Expression LambdaExpression;
}
}

View File

@@ -0,0 +1,48 @@
/*
* Yet Another Tiger Compiler (YATC)
*
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
namespace YATC.Scope
{
/// <summary>
/// Describe la existencia de un símbolo en la tabla de símbolos
/// </summary>
public abstract class TigerInfo
{
/// <summary>
/// Nombre del atributo
/// </summary>
public string Name { get; set; }
/// <summary>
/// Tipo del atributo (si es una función indica tipo de retorno)
/// </summary>
public TigerTypeHolder Holder { get; set; }
/// <summary>
/// Indica si es del sistema
/// </summary>
public bool IsStandard;
}
}

View File

@@ -0,0 +1,45 @@
/*
* Yet Another Tiger Compiler (YATC)
*
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
using System;
namespace YATC.Scope
{
public class TigerTypeInfo: TigerInfo
{
internal TigerTypeInfo()
{
}
public TigerTypeInfo(string name, TigerTypeHolder holder, bool isStandard)
{
if (holder == null)
throw new ArgumentNullException("holder");
this.Name = name;
this.Holder = holder;
this.IsStandard = isStandard;
}
}
}

View File

@@ -0,0 +1,55 @@
/*
* Yet Another Tiger Compiler (YATC)
*
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
using System.Linq.Expressions;
namespace YATC.Scope
{
public class VariableInfo : FunVarInfo
{
/// <summary>
/// Determina si el atributo es un paramétro o no
/// </summary>
public bool IsParameter { get; set; }
/// <summary>
/// Whether the variable cannot be a left value of an assignment
/// </summary>
public bool IsReadOnly { get; set; }
/// <summary>
/// Refleja una variable dentro del ejecutable para la generación de código
/// </summary>
public ParameterExpression ParameterExpression { get; set; }
public VariableInfo(string name, TigerTypeHolder holder, bool isParameter)
{
this.Name = name;
this.Holder = holder;
this.IsParameter = isParameter;
}
}
}

169
YATC/Scope/TigerScope.cs Normal file
View File

@@ -0,0 +1,169 @@
/*
* Yet Another Tiger Compiler (YATC)
*
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace YATC.Scope
{
/// <summary>
/// Implementa la tabla de símbolos junto a TigerGlobal
/// </summary>
public class TigerScope
{
public TigerScope()
{
}
private TigerScope(TigerScope parent, int parentIndex)
{
Parent = parent;
ParentIndex = parentIndex;
}
private int _index;
/// <summary>
/// Ambito padre que contiene a este scope
/// </summary>
public readonly TigerScope Parent;
/// <summary>
/// Permite ver variables del padre de 0 a ParentIndex
/// </summary>
public readonly int ParentIndex;
public bool IsRoot { get { return Parent == null; } }
private readonly List<TigerScope> _children = new List<TigerScope>();
private readonly HashSet<TigerTypeInfo> _typeInfos = new HashSet<TigerTypeInfo>();
private readonly HashSet<FunVarInfo> _funVarInfos = new HashSet<FunVarInfo>();
public TigerScope[] GetChildren()
{
return _children.ToArray();
}
public TigerScope CreateChildScope()
{
return new TigerScope(this, _index);
}
public bool Add(VariableInfo variableInfo)
{
Debug.WriteLine("Added to variable scope: {0} of type {1}",
variableInfo.Name,
variableInfo.Holder.TigerType != null ? variableInfo.Holder.TigerType.Name : "null");
return _funVarInfos.Add(variableInfo) && ++_index != 0;
}
public bool Add(FunctionInfo functionInfo)
{
Debug.WriteLine("Added to function scope: {0} of type {1}",
functionInfo.Name,
functionInfo.Holder.TigerType.Name);
return _funVarInfos.Add(functionInfo) && ++_index != 0;
}
public bool Add(TigerTypeInfo tigerTypeInfo)
{
Debug.WriteLine("Added to type scope: {0} of type {1}",
tigerTypeInfo.Name,
tigerTypeInfo.Holder.TigerType != null ? tigerTypeInfo.Holder.TigerType.Name: "null");
return _typeInfos.Add(tigerTypeInfo) && ++_index != 0;
}
public bool ContainsType(TigerType tigerType, bool localSearchOnly)
{
bool isLocal = _typeInfos.Any(x => x.Holder.TigerType.Equals(tigerType));
return isLocal || (!localSearchOnly && !IsRoot && Parent.ContainsType(tigerType, false));
}
public VariableInfo[] GetLocalVariableInfos()
{
return _funVarInfos.Where(x => x is VariableInfo).Cast<VariableInfo>().ToArray();
}
public VariableInfo FindVariableInfo(string name, bool localSearchOnly)
{
var result = _funVarInfos.FirstOrDefault(x => x.Name == name);
if (result is FunctionInfo)
return null;
return (VariableInfo)result ?? (!localSearchOnly && !IsRoot ? Parent.FindVariableInfo(name, false) : null);
}
public bool CanFindVariableInfo(string name, bool localSearchOnly)
{
return FindVariableInfo(name, localSearchOnly) != null;
}
public TigerTypeInfo[] GetLocalTypeInfos()
{
return _typeInfos.ToArray();
}
public TigerTypeInfo FindTypeInfo(string name, bool localSearchOnly)
{
var result = _typeInfos.FirstOrDefault(x => x.Name == name);
return result ?? (!localSearchOnly && !IsRoot ? Parent.FindTypeInfo(name, false) : null);
}
public bool CanFindTypeInfo(string name, bool localSearchOnly)
{
return FindTypeInfo(name, localSearchOnly) != null;
}
public FunctionInfo[] GetFunctionInfos()
{
return _funVarInfos.Where(x => x is FunctionInfo).Cast<FunctionInfo>().ToArray();
}
public FunctionInfo FindFunctionInfo(string name, bool localSearchOnly)
{
FunVarInfo result = _funVarInfos.FirstOrDefault(x => x.Name == name);
if (result is VariableInfo)
return null;
return (FunctionInfo)result ?? (!localSearchOnly && !IsRoot ? Parent.FindFunctionInfo(name, false) : null);
}
public bool CanFindFunctionInfo(string name, bool localSearchOnly)
{
return FindFunctionInfo(name, localSearchOnly) != null;
}
public FunVarInfo FindFunVarInfo(string name, bool localSearchOnly)
{
FunVarInfo result = _funVarInfos.FirstOrDefault(x => x.Name == name);
return result ?? (!localSearchOnly && !IsRoot ? Parent.FindFunctionInfo(name, false) : null);
}
public bool CanFindFunVarInfo(string name, bool localSearchOnly)
{
return FindFunVarInfo(name, localSearchOnly) != null;
}
}
}

View File

@@ -0,0 +1,38 @@
/*
* Yet Another Tiger Compiler (YATC)
*
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
namespace YATC.Scope
{
public class ArrayType: TigerType
{
public ArrayType(string name, TigerTypeHolder elementTypeHolder)
: base(name, BaseType.Array)
{
this.ElementTypeHolder = elementTypeHolder;
}
public TigerTypeHolder ElementTypeHolder { get; internal set; }
}
}

View File

@@ -0,0 +1,38 @@
/*
* Yet Another Tiger Compiler (YATC)
*
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
namespace YATC.Scope
{
public class RecordType: TigerType
{
public RecordType(string name, VariableInfo[] fieldInfos)
: base(name, BaseType.Record)
{
this.FieldInfos = fieldInfos;
}
public VariableInfo[] FieldInfos { get; internal set; }
}
}

View File

@@ -0,0 +1,105 @@
/*
* Yet Another Tiger Compiler (YATC)
*
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
using System;
using System.Diagnostics;
namespace YATC.Scope
{
public enum BaseType
{
Error = -1,
Int = 0,
String = 1,
Nil = 2,
Record = 3,
Array = 4,
Void = 5,
FillIn = 6,
Unknown = 7
}
public class TigerType
{
public string Name { get; protected set; }
public BaseType Basetype { get; private set; }
public Type CLRType { get; set; }
protected TigerType(string name, BaseType basetype)
{
this.Name = name;
this.Basetype = basetype;
}
protected TigerType(BaseType baseType)
: this(baseType.ToString().ToLowerInvariant(), baseType)
{
}
public virtual Type GetCLRType()
{
if (CLRType != null)
return CLRType;
switch (Basetype)
{
case BaseType.Int:
return typeof(int);
case BaseType.String:
return typeof(string);
case BaseType.Void:
return typeof(void);
case BaseType.Nil:
return null;
case BaseType.Array:
return (this as ArrayType).ElementTypeHolder.TigerType.GetCLRType().MakeArrayType();
default:
throw new IndexOutOfRangeException();
}
}
public static readonly TigerType Error = new TigerType(BaseType.Error);
public static readonly TigerType Int = new TigerType(BaseType.Int);
public static readonly TigerType String = new TigerType(BaseType.String);
public static readonly TigerType Nil = new TigerType(BaseType.Nil);
public static readonly TigerType Void = new TigerType(BaseType.Void);
public static readonly TigerType FillIn = new TigerType(BaseType.FillIn);
public static readonly TigerType Unknown = new TigerType(BaseType.Unknown);
public virtual bool IsAssignableTo(TigerType other)
{
bool result = this.Equals(other) ||
(this.Basetype != BaseType.Record &&
this.Basetype != BaseType.Array && this.Basetype == other.Basetype) ||
(this.Basetype == BaseType.Nil && other.Basetype == BaseType.Array) ||
(this.Basetype == BaseType.Nil && other.Basetype == BaseType.Record) ||
(this.Basetype == BaseType.Nil && other.Basetype == BaseType.String);
Debug.WriteLine("Typesystem ruled that '{0}' {2} assignable to '{1}'.",
this.Name, other.Name, result ? "is" : "is NOT");
return result;
}
}
}

View File

@@ -0,0 +1,41 @@
/*
* Yet Another Tiger Compiler (YATC)
*
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
namespace YATC.Scope
{
public class TigerTypeHolder
{
public TigerTypeHolder()
{
}
public TigerTypeHolder(TigerType tigerType)
{
this.TigerType = tigerType;
}
public TigerType TigerType;
}
}