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

View File

@@ -0,0 +1,69 @@
/*
* 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 Antlr.Runtime;
using System.Reflection.Emit;
using YATC.Scope;
namespace YATC.ASTNodes
{
class AliasDeclNode : DeclarationNode
{
public AliasDeclNode(IToken payload)
: base(payload)
{
}
public TypeNode TypeNode { get { return (TypeNode)TigerChildren[0]; } }
public override bool CheckHeader(TigerScope scope, Report report, string name)
{
this.TigerTypeInfo = new TigerTypeInfo(name, new TigerTypeHolder(), false);
scope.Add(this.TigerTypeInfo);
return true;
}
public override void CheckSemantics(TigerScope scope, Report report)
{
// type is an alias of another type, then follow.
TigerTypeInfo aliasTo = scope.FindTypeInfo(this.TypeNode.Name, false);
if (aliasTo == null)
{
report.AddError(this.Line, this.Column, "Alias to undeclared type: '{0}'.", this.TypeNode.Name);
this.IsOK = false;
return;
}
this.TigerTypeInfo.Holder.TigerType = aliasTo.Holder.TigerType;
this.IsOK = true;
}
internal override void GenerateCode(ModuleBuilder moduleBuilder)
{
// do nothing
}
}
}

View File

@@ -0,0 +1,70 @@
/*
* 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 Antlr.Runtime;
using System.Reflection.Emit;
using YATC.Scope;
namespace YATC.ASTNodes
{
internal class ArrayDeclNode: DeclarationNode
{
public ArrayDeclNode(IToken payload)
: base(payload)
{
}
private ArrayType _arrayType;
public TypeNode ElementTypeNode { get { return (TypeNode)TigerChildren[0]; } }
public override bool CheckHeader(TigerScope scope, Report report, string name)
{
this.TigerTypeInfo = new TigerTypeInfo(name, new TigerTypeHolder(), false);
scope.Add(this.TigerTypeInfo);
return true;
}
public override void CheckSemantics(TigerScope scope, Report report)
{
TigerTypeInfo elementTigerTypeInfo = scope.FindTypeInfo(this.ElementTypeNode.Name, false);
if (elementTigerTypeInfo == null)
{
report.AddError(this.Line, this.Column, "Undeclared type: array element '{0}'.", this.ElementTypeNode.Name);
this.IsOK = false;
return;
}
this.TigerTypeInfo.Holder.TigerType = _arrayType = new ArrayType(this.TigerTypeInfo.Name, elementTigerTypeInfo.Holder);
this.IsOK = true;
}
internal override void GenerateCode(ModuleBuilder moduleBuilder)
{
_arrayType.CLRType = _arrayType.ElementTypeHolder.TigerType.GetCLRType().MakeArrayType();
}
}
}

View File

@@ -0,0 +1,50 @@
/*
* 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 Antlr.Runtime;
using YATC.Scope;
namespace YATC.ASTNodes
{
abstract class DeclarationNode : LocalNode
{
protected DeclarationNode(IToken payload)
: base(payload)
{
}
public TigerTypeInfo TigerTypeInfo { get; protected set; }
public virtual bool CheckHeader(TigerScope scope, Report report, string name)
{
return true;
}
/// <summary>
/// Por defecto es false
/// </summary>
public bool IsOK { get; set; }
}
}

View File

@@ -0,0 +1,154 @@
/*
* 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 Antlr.Runtime;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection.Emit;
using YATC.Scope;
namespace YATC.ASTNodes
{
internal class FunDeclSeqNode : DeclarationNode
{
public FunDeclSeqNode(IToken payload)
: base(payload)
{
}
public FunDeclNode[] FunDeclNodes { get { return TigerChildren.Cast<FunDeclNode>().ToArray(); } }
public ParameterExpression[] FunctionClousures { get; private set; }
public Expression[] FunctionAssigns { get; private set; }
public override void CheckSemantics(TigerScope scope, Report report)
{
for (int index = 0; index < FunDeclNodes.Length; index++)
{
var funDeclNode = FunDeclNodes[index];
if (scope.CanFindFunVarInfo(funDeclNode.Name, true))
{
report.AddError(this.Line, this.Column,
"Redeclared local function or variable name: '{0}'.", funDeclNode.Name);
this.IsOK = false;
return;
}
FunctionInfo outerFunction = scope.FindFunctionInfo(funDeclNode.Name, false);
if (outerFunction != null)
if (outerFunction.IsStandard)
{
report.AddError(this.Line, this.Column,
"Redeclared standard function or procedure: '{0}'.", funDeclNode.Name);
this.IsOK = false;
return;
}
else
report.AddWarning(this.Line, this.Column,
"Function name hides outer scope variable or function: '{0}'.",
funDeclNode.Name);
// Checking not repiting parameter names
var parameterNames = new HashSet<string>();
var parameterInfo = new VariableInfo[funDeclNode.Params.Length];
for (int i = 0; i < parameterInfo.Length; i++)
{
var parameter = funDeclNode.Params[i];
if (!parameterNames.Add(parameter.Name))
{
report.AddError(this.Line, this.Column,
"Redeclared function or procedure formal parameter name: '{0}'.", parameter.Name);
this.IsOK = false;
return;
}
parameter.TypeNode.CheckSemantics(scope, report);
TigerTypeInfo paramInfo = scope.FindTypeInfo(parameter.TypeNode.Name, false);
if (paramInfo == null)
{
report.AddError(this.Line, this.Column,
"Undeclared type: Function or procedure parameter: " +
"'{0}' of formal parameter '{1}'.", parameter.TypeNode.Name, parameter.Name);
this.IsOK = false;
return;
}
parameterInfo[i] = new VariableInfo(parameter.Name, paramInfo.Holder, true);
}
// Checking return type
TigerType returnType;
if (funDeclNode.TypeNode != null)
{
TigerTypeInfo returnInfo = scope.FindTypeInfo(funDeclNode.TypeNode.Name, false);
if (returnInfo == null)
{
report.AddError(this.Line, this.Column,
"Undeclared function or procedure return type: {0}", funDeclNode.TypeNode.Name);
this.IsOK = false;
return;
}
returnType = returnInfo.Holder.TigerType;
}
else
returnType = TigerType.Void;
var functionInfo = new FunctionInfo(funDeclNode.Name, parameterInfo, new TigerTypeHolder(returnType), false);
scope.Add(functionInfo);
}
// Checking children
bool areAllOk = true;
foreach (var funDeclNode in FunDeclNodes)
{
funDeclNode.CheckSemantics(scope, report);
if (!funDeclNode.IsOK)
areAllOk = false;
}
this.IsOK = areAllOk;
}
internal override void GenerateCode(ModuleBuilder moduleBuilder)
{
FunctionClousures = new ParameterExpression[this.FunDeclNodes.Length];
FunctionAssigns = new Expression[this.FunDeclNodes.Length];
for (int i = 0; i < this.FunDeclNodes.Length; i++)
FunctionClousures[i] = this.FunDeclNodes[i].GenerateHeaderCode();
for (int i = 0; i < this.FunDeclNodes.Length; i++)
this.FunDeclNodes[i].GenerateCode(moduleBuilder);
for (int i = 0; i < this.FunDeclNodes.Length; i++)
FunctionAssigns[i] = Expression.Assign(
this.FunDeclNodes[i].FunctionInfo.LambdaExpression,
this.FunDeclNodes[i].VmExpression
);
}
}
}

View File

@@ -0,0 +1,144 @@
/*
* 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 Antlr.Runtime;
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection.Emit;
using YATC.Scope;
namespace YATC.ASTNodes
{
class FunDeclNode : DeclarationNode
{
public FunDeclNode(IToken payload)
: base(payload)
{
}
public TypeNode IdNode { get { return (TypeNode)TigerChildren[0]; } }
public TypeFieldNode[] Params
{
get { return TigerChildren[1].TigerChildren.Cast<TypeFieldNode>().ToArray(); }
}
public TypeNode TypeNode
{
get { return TigerChildren[2].TigerChildren.Length > 0 ?
(TypeNode)TigerChildren[2].TigerChildren[0] :
null; }
}
public ExpressionNode ExpressionBodyNode { get { return (ExpressionNode)TigerChildren[3]; } }
public string Name { get { return this.IdNode.Name; } }
public FunctionInfo FunctionInfo;
public override void CheckSemantics(TigerScope scope, Report report)
{
FunctionInfo = scope.FindFunctionInfo(this.Name, true);
if (FunctionInfo == null)
throw new NullReferenceException();
var innerScope = scope.CreateChildScope();
foreach (var parameterInfo in FunctionInfo.ParameterInfo)
{
if (innerScope.CanFindFunVarInfo(parameterInfo.Name, false))
report.AddWarning(this.Line, this.Column,
"Parameter name hides outer scope variable or function in '{0}': '{1}'.",
this.Name, parameterInfo.Name);
innerScope.Add(parameterInfo);
}
this.ExpressionBodyNode.CheckSemantics(innerScope, report);
// chequeando que el tipo de retorno sea el mismo que el tipo de la expresion,
// no se hace salvedad en el caso de los procedures.
if (!this.ExpressionBodyNode.TigerType.IsAssignableTo(FunctionInfo.Holder.TigerType))
{
report.AddError(this.Line, this.Column,
"Type mismatch: Function or procedure return and expression types in '{0}': " +
"Expecting '{1}' and '{2}' found.",
this.Name, FunctionInfo.Holder.TigerType.Name, this.ExpressionBodyNode.TigerType.Name);
this.IsOK = false;
return;
}
this.IsOK = true;
}
public Type DelegateType;
internal ParameterExpression GenerateHeaderCode()
{
bool funRets = FunctionInfo.Holder.TigerType.Basetype != BaseType.Void;
Type[] paramTypes = new Type[this.FunctionInfo.ParameterInfo.Length + (funRets ? 1 : 0)];
for (int i = 0; i < this.FunctionInfo.ParameterInfo.Length; i++)
paramTypes[i] = this.FunctionInfo.ParameterInfo[i].Holder.TigerType.GetCLRType();
if (funRets)
paramTypes[this.Params.Length] = this.FunctionInfo.Holder.TigerType.GetCLRType();
DelegateType = funRets
? Expression.GetFuncType(paramTypes)
: Expression.GetActionType(paramTypes);
var paramExpr = Expression.Parameter(DelegateType);
FunctionInfo.LambdaExpression = paramExpr;
return paramExpr;
}
internal override void GenerateCode(ModuleBuilder moduleBuilder)
{
var paramsExprs = new ParameterExpression[this.Params.Length];
for (int i = 0; i < Params.Length; i++)
{
paramsExprs[i] = Expression.Parameter(this.FunctionInfo.ParameterInfo[i].Holder.TigerType.GetCLRType());
FunctionInfo.ParameterInfo[i].ParameterExpression = paramsExprs[i];
}
this.ExpressionBodyNode.GenerateCode(moduleBuilder);
Expression bodyExpr = this.FunctionInfo.Holder.TigerType.Basetype == BaseType.Void
? Expression.Block(
this.ExpressionBodyNode.VmExpression,
Expression.Empty()
)
: (Expression)Expression.Convert(
ExpressionBodyNode.VmExpression,
this.FunctionInfo.Holder.TigerType.GetCLRType()
);
this.VmExpression = Expression.Lambda(
//DelegateType,
bodyExpr,
this.Name,
paramsExprs
);
}
}
}

View File

@@ -0,0 +1,117 @@
/*
* 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.Linq;
namespace YATC.ASTNodes
{
internal class Node<T>
{
public enum ColorCode
{
White = 0,
Unvisited = 0,
Black = 1,
Done = 1,
Gray = 2,
StillNotDone = 2
}
public ColorCode Color { get; private set; }
public Node<T> Parent { get; private set; }
public readonly T Value;
public Node(T value)
{
this.Value = value;
}
private readonly LinkedList<Node<T>> _pred = new LinkedList<Node<T>>();
private readonly LinkedList<Node<T>> _succ = new LinkedList<Node<T>>();
public IEnumerable<Node<T>> Pred { get { return _pred; } }
public IEnumerable<Node<T>> Succ { get { return _succ; } }
public IEnumerable<Node<T>> Adj { get { return _pred.Concat(_succ); } }
public void AddSucc(Node<T> node) { _succ.AddLast(node); }
public void AddPred(Node<T> node) { _pred.AddLast(node); }
private static Node<T> DFS(IEnumerable<Node<T>> nodes, bool checkCycles, out LinkedList<Node<T>> linkedList)
{
int time = 0;
linkedList = new LinkedList<Node<T>>();
foreach (var node in nodes.Where(node => node.Color == ColorCode.White))
{
var cycleEnd = node.Visit(ref time, checkCycles, linkedList);
if (cycleEnd != null)
return cycleEnd;
}
return null;
}
private Node<T> Visit(ref int time, bool checkCycles, LinkedList<Node<T>> topologicalSort)
{
this.Color = ColorCode.Gray;
foreach (var node in this.Succ)
{
// by the Proof of Theorem 22.12, page 614, (u,v) is a back edge iff v is Gray
// from Cormen et al. - Introduction To Algorithms, 3rd edition
if (node.Color == ColorCode.Gray && checkCycles)
return this;
if (node.Color == ColorCode.White)
{
node.Parent = this;
var cycleEnd = node.Visit(ref time, checkCycles, topologicalSort);
if (cycleEnd != null)
return cycleEnd;
}
}
this.Color = ColorCode.Black;
topologicalSort.AddFirst(this);
return null;
}
public static Node<T> TopologicalSort(IEnumerable<Node<T>> nodes, out LinkedList<Node<T>> linkedList)
{
return DFS(nodes, true, out linkedList);
}
public class NodeValueEqualityComparer : IEqualityComparer<Node<T>>
{
public bool Equals(Node<T> x, Node<T> y)
{
return x.Value.Equals(y.Value);
}
public int GetHashCode(Node<T> obj)
{
return obj.Value.GetHashCode();
}
}
}
}

View File

@@ -0,0 +1,125 @@
/*
* 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 Antlr.Runtime;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using YATC.Scope;
namespace YATC.ASTNodes
{
internal class RecordDeclNode : DeclarationNode
{
public RecordDeclNode(IToken payload)
: base(payload)
{
}
public TypeFieldNode[] TypeFieldNodes
{
get
{
return TigerChildren.Length > 0 ? TigerChildren.Cast<TypeFieldNode>().ToArray() : new TypeFieldNode[] {};
}
}
public TypeBuilder TypeBuilder { get; private set; }
public override bool CheckHeader(TigerScope scope, Report report, string name)
{
this.TigerTypeInfo = new TigerTypeInfo(name, new TigerTypeHolder(), false);
scope.Add(this.TigerTypeInfo);
return true;
}
public override void CheckSemantics(TigerScope scope, Report report)
{
var hash = new HashSet<string>();
foreach (var typeFieldNode in this.TypeFieldNodes)
{
typeFieldNode.TypeNode.CheckSemantics(scope, report);
if (!typeFieldNode.TypeNode.IsOk)
{
this.TigerTypeInfo.Holder.TigerType = TigerType.Error;
this.IsOK = false;
return;
}
string name = typeFieldNode.IdNode.Name;
if (!hash.Add(name))
{
report.AddError(this.Line, this.Column, "Redeclared field name: '{0}'.", name);
this.TigerTypeInfo.Holder.TigerType = TigerType.Error;
this.IsOK = false;
return;
}
}
VariableInfo[] fieldInfos = new VariableInfo[this.TypeFieldNodes.Length];
for (int i = 0; i < fieldInfos.Length; i++)
{
TigerTypeInfo fieldTigerTypeInfo = scope.FindTypeInfo(this.TypeFieldNodes[i].TypeNode.Name, false);
if (fieldTigerTypeInfo == null)
{
report.AddError(this.Line, this.Column, "Undeclared field type: '{0}'.",
this.TypeFieldNodes[i].TypeNode.Name);
this.TigerTypeInfo.Holder.TigerType = TigerType.Error;
this.IsOK = false;
return;
}
fieldInfos[i] = new VariableInfo(this.TypeFieldNodes[i].Name, fieldTigerTypeInfo.Holder, false);
}
this.TigerTypeInfo.Holder.TigerType = new RecordType(this.TigerTypeInfo.Name, fieldInfos);
this.IsOK = true;
}
public void GenerateHeaderCode(ModuleBuilder moduleBuilder)
{
string name = string.Format("{0}_{1}", this.TigerTypeInfo.Name, ProgramNode.RecordNumber++);
this.TigerTypeInfo.Holder.TigerType.CLRType =
this.TypeBuilder = moduleBuilder.DefineType(name, TypeAttributes.Public);
}
internal override void GenerateCode(ModuleBuilder moduleBuilder)
{
foreach (var variableInfo in ((RecordType)this.TigerTypeInfo.Holder.TigerType).FieldInfos)
{
this.TypeBuilder.DefineField(
variableInfo.Name,
variableInfo.Holder.TigerType.GetCLRType(),
FieldAttributes.Public);
}
// Finish the type.
this.TigerTypeInfo.Holder.TigerType.CLRType = this.TypeBuilder.CreateType();
}
}
}

View File

@@ -0,0 +1,190 @@
/*
* 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 Antlr.Runtime;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using YATC.Scope;
namespace YATC.ASTNodes
{
internal class TypeDeclSeqNode : DeclarationNode
{
private LinkedList<Node<TypeDeclNode>> _topologicalSort;
public TypeDeclSeqNode(IToken payload)
: base(payload)
{
}
public IEnumerable<TypeDeclNode> TypeDeclNodes { get { return TigerChildren.Cast<TypeDeclNode>(); } }
/// <summary>
/// Por pasos:
/// 1ro : Agregar cabezas de tipos
/// 2do : Construir grafo y revisar orden topologico
/// 3ro : En el orden dado hacer chequeo semantico
/// </summary>
/// <param name="scope"></param>
/// <param name="report"></param>
public override void CheckSemantics(TigerScope scope, Report report)
{
var checkedNodes = new Dictionary<string, TypeDeclNode>();
// 1. agregar cabezas de tipos
foreach (var typeDeclNode in TypeDeclNodes)
{
if (!checkedNodes.ContainsKey(typeDeclNode.Name))
checkedNodes.Add(typeDeclNode.Name, typeDeclNode);
else
{
report.AddError(this.Line, this.Column,
"Redeclared name in type declaration sequence: '{0}'.", typeDeclNode.Name);
this.IsOK = false;
return;
}
// chequeamos el header, mayormente por problemas de redeclaracion local
// o global pero de tipos standard
if (!typeDeclNode.CheckHeader(scope, report, string.Empty))
{
this.IsOK = false;
return;
}
}
// 2. DAG
// 2.1 construir grafo
var graph = new Dictionary<TypeDeclNode, Node<TypeDeclNode>>();
foreach (var typeDeclNode in TypeDeclNodes)
{
string edgeNameTo = string.Empty;
if (typeDeclNode.IsAliasNode)
edgeNameTo = ((AliasDeclNode)typeDeclNode.DeclarationNode).TypeNode.Name;
else if (typeDeclNode.IsArrayNode)
edgeNameTo = ((ArrayDeclNode)typeDeclNode.DeclarationNode).ElementTypeNode.Name;
Node<TypeDeclNode> thisNode;
if (!graph.TryGetValue(typeDeclNode, out thisNode))
{
thisNode = new Node<TypeDeclNode>(typeDeclNode);
graph.Add(typeDeclNode, thisNode);
}
if (edgeNameTo == string.Empty)
continue;
TypeDeclNode edgeTo;
if (checkedNodes.TryGetValue(edgeNameTo, out edgeTo))
{
Node<TypeDeclNode> node;
if (!graph.TryGetValue(edgeTo, out node))
{
node = new Node<TypeDeclNode>(edgeTo);
graph.Add(edgeTo, node);
}
node.AddSucc(thisNode);
}
}
// 2.2 obtener orden topologico (detectando ciclos)
#if DEBUG
foreach (var edge in graph.Values)
{
var from = edge.Succ.Any() ? edge.Succ.FirstOrDefault().Value.IdNode.ToString() : "<end>";
var to = edge.Value.IdNode;
Debug.WriteLine("{0} -> {1}", from, to);
}
#endif
var cycleEnd = Node<TypeDeclNode>.TopologicalSort(graph.Values, out _topologicalSort);
#if DEBUG
foreach (var node in _topologicalSort)
Debug.WriteLine("Adding {0}", node.Value.IdNode);
#endif
if (cycleEnd != null)
{
var sb = new StringBuilder();
for (var current = cycleEnd; current != null; current = current.Parent)
sb.Append(current.Value.IdNode.Name + " -> ");
sb.Append(cycleEnd.Value.IdNode.Name);
report.AddError(this.Line, this.Column,
"Undetected record in recursive type declaration sequence. Cycle definition is: {0}",
sb.ToString());
this.IsOK = false;
return;
}
// 3. chequear semantica de los nodos en el orden topologico
foreach (var node in _topologicalSort)
{
Debug.WriteLine("Checking {0}", node.Value.IdNode);
node.Value.CheckSemantics(scope, report);
if (!node.Value.IsOK)
{
this.IsOK = false;
return;
}
}
//foreach (var node in linkedList)
//{
// node.
//}
this.IsOK = true;
}
internal override void GenerateCode(ModuleBuilder moduleBuilder)
{
// records go first
IEnumerable<RecordDeclNode> recordDeclNodes =
_topologicalSort.Select(x => x.Value).
Where(x => x.DeclarationNode is RecordDeclNode).
Select(x => x.DeclarationNode).
Cast<RecordDeclNode>();
foreach (var recordDeclNode in recordDeclNodes)
recordDeclNode.GenerateHeaderCode(moduleBuilder);
// everything
foreach (var declarationNode in _topologicalSort.Select(x => x.Value))
declarationNode.GenerateCode(moduleBuilder);
}
}
}

View File

@@ -0,0 +1,84 @@
/*
* 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 Antlr.Runtime;
using System.Reflection.Emit;
using YATC.Scope;
namespace YATC.ASTNodes
{
class TypeDeclNode : DeclarationNode
{
public TypeDeclNode(IToken payload)
: base(payload)
{
}
public string Name { get { return this.IdNode.Name; } }
public IdNode IdNode { get { return (IdNode)TigerChildren[0]; } }
public DeclarationNode DeclarationNode { get { return (DeclarationNode)TigerChildren[1]; } }
public bool IsAliasNode { get { return this.DeclarationNode is AliasDeclNode; } }
public bool IsRecordNode { get { return this.DeclarationNode is RecordDeclNode; } }
public bool IsArrayNode { get { return this.DeclarationNode is ArrayDeclNode; } }
public override bool CheckHeader(TigerScope scope, Report report, string name)
{
if (scope.CanFindTypeInfo(this.Name, true))
{
report.AddError(this.Line, this.Column, "Redeclared local type: '{0}'.", this.Name);
this.IsOK = false;
return false;
}
TigerTypeInfo outerTypeInfo = scope.FindTypeInfo(this.Name, false);
if (outerTypeInfo != null)
if (outerTypeInfo.IsStandard)
{
report.AddError(this.Line, this.Column,
"Redeclared standard type: '{0}'.",
this.Name);
this.IsOK = false;
return false;
}
else
report.AddWarning(this.Line, this.Column, "Type name hides outer scope type: '{0}'.", this.Name);
return this.DeclarationNode.CheckHeader(scope, report, this.IdNode.Name);
}
public override void CheckSemantics(TigerScope scope, Report report)
{
this.DeclarationNode.CheckSemantics(scope, report);
this.IsOK = this.DeclarationNode.IsOK;
}
internal override void GenerateCode(ModuleBuilder moduleBuilder)
{
this.DeclarationNode.GenerateCode(moduleBuilder);
}
}
}

View File

@@ -0,0 +1,140 @@
/*
* 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 Antlr.Runtime;
using System.Linq.Expressions;
using System.Reflection.Emit;
using YATC.Scope;
namespace YATC.ASTNodes
{
class VarDeclNode : DeclarationNode
{
public VarDeclNode(IToken payload)
: base(payload)
{
}
public TypeNode IdNode { get { return (TypeNode)TigerChildren[0]; } }
public TypeNode TypeNode { get { return (TypeNode)TigerChildren[1]; } }
public ExpressionNode RightExpressionNode { get { return (ExpressionNode)TigerChildren[2]; } }
public VariableInfo VariableInfo { get; set; }
public bool IsAutoVariable { get { return this.TypeNode is FillInTypeNode; } }
public override void CheckSemantics(TigerScope scope, Report report)
{
string name = this.IdNode.Name;
if (scope.CanFindFunVarInfo(name, true))
{
report.AddError(this.Line, this.Column,
"Redeclared local variable or function: '{0}'.",
name);
this.IsOK = false;
return;
}
FunctionInfo outerfunctionInfo = scope.FindFunctionInfo(name, false);
if (outerfunctionInfo != null)
{
if (outerfunctionInfo.IsStandard)
{
report.AddError(this.Line, this.Column,
"Cannot define variable name with standard function: '{0}'.",
name);
this.IsOK = false;
return;
}
else
report.AddWarning(this.Line, this.Column,
"Variable name hides outer scope variable or function: '{0}'.",
name);
}
this.RightExpressionNode.CheckSemantics(scope, report);
if (!this.RightExpressionNode.IsOk)
{
this.IsOK = false;
return;
}
if (this.RightExpressionNode.TigerType.Equals(TigerType.Void))
{
report.AddError(this.Line, this.Column, "Right hand side expression must evaluate to a returning value.");
this.IsOK = false;
return;
}
this.TypeNode.CheckSemantics(scope, report);
TigerType returnType = this.RightExpressionNode.TigerType;
if (!this.IsAutoVariable)
{
TigerTypeInfo tigerTypeInfo = scope.FindTypeInfo(this.TypeNode.Name, false);
if (tigerTypeInfo == null)
{
report.AddError(this.Line, this.Column, "Undeclared type: '{0}'.", this.TypeNode.Name);
this.IsOK = false;
return;
}
/* Assignment */
if (!this.RightExpressionNode.TigerType.IsAssignableTo(tigerTypeInfo.Holder.TigerType))
{
report.AddError(this.Line, this.Column,
"Type mismatch: Variable declaration and expression do not match " +
"for '{0}': expecting '{1}' and '{2}' found.",
name, tigerTypeInfo.Holder.TigerType.Name, this.RightExpressionNode.TigerType.Name);
this.IsOK = false;
return;
}
returnType = tigerTypeInfo.Holder.TigerType;
}
else
{
if (returnType.Basetype == BaseType.Nil)
{
report.AddError(this.Line, this.Column,
"An automatic variable cannot be declared from nil expression.");
this.IsOK = false;
return;
}
}
this.VariableInfo = new VariableInfo(name, new TigerTypeHolder(returnType), false);
scope.Add(VariableInfo);
this.IsOK = true;
}
internal override void GenerateCode(ModuleBuilder moduleBuilder)
{
this.RightExpressionNode.GenerateCode(moduleBuilder);
this.VariableInfo.ParameterExpression =
Expression.Parameter(this.VariableInfo.Holder.TigerType.GetCLRType());
}
}
}

View File

@@ -0,0 +1,144 @@
/*
* 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 Antlr.Runtime;
using System;
using System.Linq.Expressions;
using System.Reflection.Emit;
using YATC.Scope;
namespace YATC.ASTNodes
{
class ArrayInstNode : ExpressionNode
{
private ArrayType _arrayType;
public ArrayInstNode(IToken payload)
: base(payload)
{
}
public IdNode IdNode { get { return (IdNode)TigerChildren[0]; } }
public ExpressionNode IndexExpressionNode { get { return (ExpressionNode)TigerChildren[1]; } }
public ExpressionNode OfExpressionNode { get { return (ExpressionNode)TigerChildren[2]; } }
public override void CheckSemantics(TigerScope scope, Report report)
{
TigerTypeInfo arrayInfo = scope.FindTypeInfo(this.IdNode.Name, false);
if (arrayInfo == null || arrayInfo.Holder == null || arrayInfo.Holder.TigerType == null)
{
report.AddError(this.Line, this.Column, "Undeclared array type: '{0}'.", this.IdNode.Name);
this.TigerType = TigerType.Error;
return;
}
if (arrayInfo.Holder.TigerType.Basetype != BaseType.Array)
{
report.AddError(this.Line, this.Column, "Given type is not an array: {0}", this.IdNode.Name);
this.TigerType = TigerType.Error;
return;
}
_arrayType = arrayInfo.Holder.TigerType as ArrayType;
if (_arrayType == null)
throw new NullReferenceException();
this.IndexExpressionNode.CheckSemantics(scope, report);
if (!this.IndexExpressionNode.IsOk)
{
this.TigerType = TigerType.Error;
return;
}
if (this.IndexExpressionNode.TigerType.Basetype != BaseType.Int)
{
report.AddError(this.Line, this.Column, "Type mismatch: Given index expression is not an integer: {0}", this.IndexExpressionNode.TigerType.Name);
this.TigerType = TigerType.Error;
return;
}
this.OfExpressionNode.CheckSemantics(scope, report);
if (!this.OfExpressionNode.IsOk)
{
this.TigerType = TigerType.Error;
return;
}
if (!this.OfExpressionNode.TigerType.IsAssignableTo(_arrayType.ElementTypeHolder.TigerType))
{
report.AddError(this.Line, this.Column, "Type mismatch: Array element and expression types: '{0}' and '{1}'",
_arrayType.ElementTypeHolder.TigerType.Name, this.OfExpressionNode.TigerType.Name);
this.TigerType = TigerType.Error;
return;
}
this.TigerType = _arrayType;
}
internal override void GenerateCode(ModuleBuilder moduleBuilder)
{
this.IndexExpressionNode.GenerateCode(moduleBuilder);
this.OfExpressionNode.GenerateCode(moduleBuilder);
Type elementType = _arrayType.ElementTypeHolder.TigerType.GetCLRType();
Type arrayType = elementType.MakeArrayType();
ParameterExpression arrayParamExpr = Expression.Parameter(arrayType);
ParameterExpression counterExpr = Expression.Parameter(typeof(int));
var arrayInitExpr = Expression.NewArrayBounds(elementType, this.IndexExpressionNode.VmExpression);
LabelTarget breakLabel = Expression.Label();
BlockExpression blockInitExpression = Expression.Block(
new ParameterExpression[] { arrayParamExpr, counterExpr },
new Expression[]
{
Expression.Assign(arrayParamExpr, arrayInitExpr),
Expression.Assign(counterExpr, Expression.Constant(0)),
Expression.Loop(
Expression.Block(
Expression.IfThenElse(
Expression.LessThan(
counterExpr,
this.IndexExpressionNode.VmExpression),
Expression.Assign(
Expression.ArrayAccess(arrayParamExpr, counterExpr),
Expression.Convert(
this.OfExpressionNode.VmExpression,
elementType
)
),
Expression.Break(breakLabel)
),
Expression.Assign(counterExpr, Expression.Increment(counterExpr))
), breakLabel),
arrayParamExpr
}
);
this.VmExpression = blockInitExpression;
}
}
}

View File

@@ -0,0 +1,40 @@
/*
* 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 Antlr.Runtime;
using YATC.Scope;
namespace YATC.ASTNodes
{
public abstract class AccessNode: AtomicNode
{
protected AccessNode(IToken payload)
: base(payload)
{
}
public TigerType ParentType { get; set; }
}
}

View File

@@ -0,0 +1,99 @@
/*
* 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 Antlr.Runtime;
using System.Linq.Expressions;
using System.Reflection.Emit;
using YATC.Scope;
namespace YATC.ASTNodes
{
public class ArrayAccessNode : AccessNode
{
public ArrayAccessNode(IToken payload)
: base(payload)
{
}
public ExpressionNode IndexExpressionNode { get { return (ExpressionNode) TigerChildren[0]; } }
public AccessNode AccessNode { get { return TigerChildren.Length > 1 ? (AccessNode)TigerChildren[1] : null; } }
public override void CheckSemantics(TigerScope scope, Report report)
{
this.IndexExpressionNode.CheckSemantics(scope, report);
if (!this.IndexExpressionNode.IsOk)
{
this.TigerType = TigerType.Error;
return;
}
if (this.IndexExpressionNode.TigerType.Basetype != BaseType.Int)
{
this.TigerType = TigerType.Error;
report.AddError(this.Line, this.Column,
"Type mismatch: Cannot index with a non-integer expression: '{0}' was found.",
this.IndexExpressionNode.TigerType.Name);
return;
}
if (this.ParentType.Basetype != BaseType.Array)
{
report.AddError(this.Line, this.Column,
"Type mismatch: Variable or field is not an array: '{0}' was found.",
this.ParentType.Name);
this.TigerType = TigerType.Error;
return;
}
TigerType parentType = ((ArrayType)this.ParentType).ElementTypeHolder.TigerType;
if (this.AccessNode == null)
{
this.TigerType = parentType;
}
else
{
this.AccessNode.ParentType = parentType;
this.AccessNode.CheckSemantics(scope, report);
this.TigerType = AccessNode.TigerType;
}
}
internal override void GenerateCode(ModuleBuilder moduleBuilder)
{
this.IndexExpressionNode.GenerateCode(moduleBuilder);
this.VmExpression = Expression.ArrayAccess(
this.VmExpression,
this.IndexExpressionNode.VmExpression
);
if (this.AccessNode == null)
return;
this.AccessNode.VmExpression = this.VmExpression;
this.AccessNode.GenerateCode(moduleBuilder);
this.VmExpression = this.AccessNode.VmExpression;
}
}
}

View File

@@ -0,0 +1,107 @@
/*
* 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 Antlr.Runtime;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection.Emit;
using YATC.Scope;
namespace YATC.ASTNodes
{
class FieldAccessNode : AccessNode
{
private TigerType _parentType;
public FieldAccessNode(IToken payload)
: base(payload)
{
}
public TypeNode IdNode { get { return (TypeNode)TigerChildren[0]; } }
public AccessNode AccessNode { get { return TigerChildren.Length > 1 ? (AccessNode)TigerChildren[1] : null; } }
public override void CheckSemantics(TigerScope scope, Report report)
{
if (this.ParentType.Basetype != BaseType.Record)
{
report.AddError(this.Line, this.Column,
"Type mismatch: Variable or field is not a record: '{0}' was found.",
this.ParentType.Name);
this.TigerType = TigerType.Error;
return;
}
VariableInfo[] fieldInfos = ((RecordType)this.ParentType).FieldInfos;
if (fieldInfos.Length == 0)
{
report.AddError(this.Line, this.Column,
"Cannot access field on empty record type '{0}': '{1}'",
this.ParentType.Name, this.IdNode.Name);
this.TigerType = TigerType.Error;
return;
}
var fieldInfo = fieldInfos.FirstOrDefault(x => x.Name == this.IdNode.Name);
if (fieldInfo == null)
{
report.AddError(this.Line, this.Column,
"Record type '{0}' does not contain a definition for '{1}'.",
this.ParentType.Name, this.IdNode.Name);
this.TigerType = TigerType.Error;
return;
}
_parentType = fieldInfo.Holder.TigerType;
if (this.AccessNode == null)
{
this.TigerType = _parentType;
}
else
{
this.AccessNode.ParentType = _parentType;
this.AccessNode.CheckSemantics(scope, report);
this.TigerType = AccessNode.TigerType;
}
}
internal override void GenerateCode(ModuleBuilder moduleBuilder)
{
this.VmExpression = Expression.MakeMemberAccess(
this.VmExpression,
this.ParentType.GetCLRType().GetMember(this.IdNode.Name)[0]
);
if (this.AccessNode == null)
return;
this.AccessNode.VmExpression = this.VmExpression;
this.AccessNode.GenerateCode(moduleBuilder);
this.VmExpression = this.AccessNode.VmExpression;
}
}
}

View File

@@ -0,0 +1,37 @@
/*
* 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 Antlr.Runtime;
namespace YATC.ASTNodes
{
public abstract class AtomicNode : ExpressionNode
{
protected AtomicNode(IToken payload)
: base(payload)
{
}
}
}

View File

@@ -0,0 +1,89 @@
/*
* 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 Antlr.Runtime;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection.Emit;
using YATC.Scope;
namespace YATC.ASTNodes
{
class ExprSeqNode : AtomicNode
{
public ExprSeqNode(IToken payload)
: base(payload)
{
}
/// <summary>
/// Says whether the node contains a BreakNode, that is not within the control of a cycle control structure.
/// </summary>
public bool HasBreakInside { get; internal set; }
public ExpressionNode[] ExpressionNodes
{
get { return TigerChildren.Length > 0 ?
TigerChildren.Cast<ExpressionNode>().ToArray() : null; }
}
public override void CheckSemantics(TigerScope scope, Report report)
{
if (this.ExpressionNodes == null)
{
this.TigerType = TigerType.Void;
return;
}
bool allOk = true;
foreach (var expressionNode in this.ExpressionNodes)
{
expressionNode.CheckSemantics(scope, report);
if (!expressionNode.IsOk)
allOk = false;
}
this.TigerType = allOk
? ((this.HasBreakInside || ExpressionNodes.Length == 0)
? TigerType.Void
: ExpressionNodes[ExpressionNodes.Length - 1].TigerType)
: TigerType.Error;
}
internal override void GenerateCode(ModuleBuilder moduleBuilder)
{
if (this.ExpressionNodes != null)
{
foreach (var expressionNode in this.ExpressionNodes)
expressionNode.GenerateCode(moduleBuilder);
this.VmExpression = Expression.Block(new ParameterExpression[] { },
this.ExpressionNodes.Select(x => x.VmExpression ?? Expression.Empty()));
}
else
this.VmExpression = Expression.Empty();
}
}
}

View File

@@ -0,0 +1,137 @@
/*
* 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 Antlr.Runtime;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection.Emit;
using YATC.Scope;
namespace YATC.ASTNodes
{
class FunCallNode : AtomicNode
{
public FunCallNode(IToken payload)
: base(payload)
{
}
public TypeNode IdNode { get { return (TypeNode)TigerChildren[0]; } }
public ExpressionNode[] ActualParametersNodes
{
get
{
return TigerChildren.Length > 1 ?
TigerChildren[1].TigerChildren.Cast<ExpressionNode>().ToArray() :
new ExpressionNode[] {};
}
}
public FunctionInfo FunctionInfo { get; set; }
public override void CheckSemantics(TigerScope scope, Report report)
{
FunctionInfo functionInfo = scope.FindFunctionInfo(this.IdNode.Name, false);
this.FunctionInfo = functionInfo;
if (functionInfo == null)
{
report.AddError(this.Line, this.Column, "Undeclared function: '{0}'.", this.IdNode.Name);
this.TigerType = TigerType.Error;
return;
}
if (this.ActualParametersNodes == null && functionInfo.ParameterInfo.Length > 0 ||
this.ActualParametersNodes != null && this.ActualParametersNodes.Length != functionInfo.ParameterInfo.Length)
{
report.AddError(this.Line, this.Column,
"Length of formal and actual parameters differ for function or procedure '{0}': " +
"Expecting {1} and found {2} arguments.",
this.IdNode.Name,
functionInfo.ParameterInfo.Length,
this.ActualParametersNodes != null ? this.ActualParametersNodes.Length : 0);
this.TigerType = TigerType.Error;
return;
}
if (this.ActualParametersNodes != null)
{
for (int i = 0; i < functionInfo.ParameterInfo.Length; i++)
{
this.ActualParametersNodes[i].CheckSemantics(scope, report);
if (!this.ActualParametersNodes[i].IsOk)
{
this.TigerType = TigerType.Error;
return;
}
if (!this.ActualParametersNodes[i].TigerType.IsAssignableTo(functionInfo.ParameterInfo[i].Holder.TigerType))
{
report.AddError(this.Line, this.Column,
"Types mismatch: Formal and actual parameter types differ for argument number {0} whilst calling " +
"function or procedure '{3}': Expecting '{1}' and found '{2}'.",
i,
functionInfo.ParameterInfo[i].Holder.TigerType.Name,
this.ActualParametersNodes[i].TigerType.Name,
this.IdNode.Name);
this.TigerType = TigerType.Error;
return;
}
}
}
this.TigerType = functionInfo.Holder.TigerType;
}
internal override void GenerateCode(ModuleBuilder moduleBuilder)
{
Expression[] arguments = new Expression[this.ActualParametersNodes.Length];
for (int i = 0; i < this.ActualParametersNodes.Length; i++)
{
ExpressionNode argument = this.ActualParametersNodes[i];
argument.GenerateCode(moduleBuilder);
arguments[i] = Expression.Convert(
argument.VmExpression,
this.FunctionInfo.ParameterInfo[i].Holder.TigerType.GetCLRType()
);
}
if (this.FunctionInfo.Name == "exit")
{
var exit = ((Action<int>)Environment.Exit).Method;
this.VmExpression = Expression.Call(exit, arguments[0]);
return;
}
if (this.FunctionInfo.MethodInfo != null)
this.VmExpression = Expression.Call(this.FunctionInfo.MethodInfo, arguments);
else
this.VmExpression = Expression.Invoke(this.FunctionInfo.LambdaExpression, arguments);
}
}
}

View File

@@ -0,0 +1,138 @@
/*
* 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 Antlr.Runtime;
using System.Linq.Expressions;
using System.Reflection.Emit;
using YATC.Scope;
namespace YATC.ASTNodes
{
class IfNode : AtomicNode
{
public IfNode(IToken payload)
: base(payload)
{
}
public ExpressionNode ConditionNode { get { return (ExpressionNode)TigerChildren[0]; } }
public ExpressionNode ThenExpressionNode { get { return (ExpressionNode)TigerChildren[1]; } }
public ExpressionNode ElseExpressionNode
{
get
{
return TigerChildren.Length > 2 ? (ExpressionNode)TigerChildren[2] : null;
}
}
public override void CheckSemantics(TigerScope scope, Report report)
{
this.ConditionNode.CheckSemantics(scope, report);
this.ThenExpressionNode.CheckSemantics(scope, report);
bool allOk = this.ConditionNode.IsOk && this.ThenExpressionNode.IsOk;
if (this.ConditionNode.IsOk)
{
if (this.ConditionNode.TigerType.Basetype == BaseType.Void)
{
report.AddError(this.Line, this.Column,
"Type mismatch: Invalid use of if condition with a non-valued expression.",
this.ConditionNode.TigerType.Name);
this.TigerType = TigerType.Error;
allOk = false;
}
else
if (this.ConditionNode.TigerType.Basetype != BaseType.Int)
{
report.AddError(this.Line, this.Column,
"Type mismatch: Invalid use of if condition with a non-int expression: '{0}' was found.",
this.ConditionNode.TigerType.Name);
this.TigerType = TigerType.Error;
allOk = false;
}
}
TigerType returnType = this.ThenExpressionNode.TigerType;
if (this.ElseExpressionNode != null)
{
this.ElseExpressionNode.CheckSemantics(scope, report);
allOk &= this.ElseExpressionNode.IsOk;
bool isThenAssignable =
this.ThenExpressionNode.TigerType.IsAssignableTo(this.ElseExpressionNode.TigerType);
returnType = isThenAssignable ? this.ElseExpressionNode.TigerType : this.ThenExpressionNode.TigerType;
/* facilita que se puede asignar record y nil, con independencia del orden en que aparezca */
if (!isThenAssignable && !this.ElseExpressionNode.TigerType.IsAssignableTo(this.ThenExpressionNode.TigerType))
{
report.AddError(this.Line, this.Column,
"Type mismatch: The then and else expression types of an if-then-else differ: " +
"'{0}' and '{1}' were found.",
this.ThenExpressionNode.TigerType.Name, this.ElseExpressionNode.TigerType.Name);
this.TigerType = TigerType.Error;
return;
}
}
else
{
if (this.ThenExpressionNode.TigerType.Basetype != BaseType.Void)
{
report.AddError(this.Line, this.Column,
"Type mismatch: The then expression at a if-then statement must not return a value: " +
"Found '{0}' whilst expecting void.",
this.ThenExpressionNode.TigerType.Name);
this.TigerType = TigerType.Error;
return;
}
}
this.TigerType = allOk ? returnType : TigerType.Error;
}
internal override void GenerateCode(ModuleBuilder moduleBuilder)
{
this.ConditionNode.GenerateCode(moduleBuilder);
this.ThenExpressionNode.GenerateCode(moduleBuilder);
if (this.ElseExpressionNode != null)
{
this.ElseExpressionNode.GenerateCode(moduleBuilder);
this.VmExpression = Expression.Condition(
Expression.NotEqual(this.ConditionNode.VmExpression, Expression.Constant(0)),
this.ThenExpressionNode.VmExpression,
this.ElseExpressionNode.VmExpression);
}
else
{
this.VmExpression = Expression.IfThen(
Expression.NotEqual(this.ConditionNode.VmExpression, Expression.Constant(0)),
this.ThenExpressionNode.VmExpression);
}
}
}
}

View File

@@ -0,0 +1,58 @@
/*
* 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 Antlr.Runtime;
using System.Linq.Expressions;
using System.Reflection.Emit;
using YATC.Scope;
namespace YATC.ASTNodes
{
class IntNode : AtomicNode
{
public IntNode(IToken payload)
: base(payload)
{
}
public int Value { get { return int.Parse(this.Text); } }
public override void CheckSemantics(TigerScope scope, Report report)
{
int value;
if (!int.TryParse(this.Text, out value))
{
report.AddError(this.Line, this.Column, "Integer literal value is out of range.");
this.TigerType = TigerType.Error;
}
else this.TigerType = TigerType.Int;
}
internal override void GenerateCode(ModuleBuilder moduleBuilder)
{
this.VmExpression = Expression.Constant(this.Value);
}
}
}

View File

@@ -0,0 +1,131 @@
/*
* 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 Antlr.Runtime;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection.Emit;
using YATC.Scope;
namespace YATC.ASTNodes
{
class LetNode : AtomicNode
{
public LetNode(IToken payload)
: base(payload)
{
}
public IEnumerable<DeclarationNode> DeclarationNodes { get { return TigerChildren[0].TigerChildren.Cast<DeclarationNode>(); } }
public ExprSeqNode ExprSeqNode
{
get
{
return (ExprSeqNode)TigerChildren[1];
}
}
public override void CheckSemantics(TigerScope scope, Report report)
{
TigerScope innerScope = scope.CreateChildScope();
foreach (var declarationNode in this.DeclarationNodes)
{
declarationNode.CheckSemantics(innerScope, report);
if (!declarationNode.IsOK)
{
this.TigerType = TigerType.Error;
return;
}
}
this.ExprSeqNode.CheckSemantics(innerScope, report);
if (!this.ExprSeqNode.IsOk || scope.ContainsType(this.ExprSeqNode.TigerType, false))
this.TigerType = this.ExprSeqNode.TigerType;
else
{
report.AddError(this.Line, this.Column,
"Type mismatch: Type '{0}' returned from let declaration is not " +
"defined in an outer scope, or it is a different definition.",
this.ExprSeqNode.TigerType.Name);
this.TigerType = TigerType.Error;
}
}
internal override void GenerateCode(ModuleBuilder moduleBuilder)
{
foreach (var declarationNode in DeclarationNodes)
declarationNode.GenerateCode(moduleBuilder);
if (this.ExprSeqNode.ExpressionNodes != null)
foreach (var expressionNode in this.ExprSeqNode.ExpressionNodes)
expressionNode.GenerateCode(moduleBuilder);
// variables
IEnumerable<VarDeclNode> varDeclNodes =
this.DeclarationNodes.Where(x => x is VarDeclNode).Cast<VarDeclNode>();
IEnumerable<ParameterExpression> variables =
varDeclNodes.Select(x => x.VariableInfo.ParameterExpression);
IEnumerable<Expression> initVariablesExpressions =
varDeclNodes.Select(varDeclNode =>
Expression.Assign(
varDeclNode.VariableInfo.ParameterExpression,
Expression.Convert(
varDeclNode.RightExpressionNode.VmExpression,
varDeclNode.VariableInfo.Holder.TigerType.GetCLRType()
)
)
);
// fundeclseq
IEnumerable<FunDeclSeqNode> funDeclSeqNodes =
this.DeclarationNodes.Where(x => x is FunDeclSeqNode).Cast<FunDeclSeqNode>();
// final
IEnumerable<Expression> blockExpressions = initVariablesExpressions.Concat(
this.ExprSeqNode.ExpressionNodes != null ?
(this.ExprSeqNode.ExpressionNodes.Select(x => x.VmExpression)) :
new Expression[] { Expression.Empty() }
);
var functionClousures = new List<ParameterExpression>();
var functionAssigns = new List<Expression>();
foreach (var funDeclSeqNode in funDeclSeqNodes)
{
functionClousures.AddRange(funDeclSeqNode.FunctionClousures);
functionAssigns.AddRange(funDeclSeqNode.FunctionAssigns);
}
this.VmExpression = Expression.Block(
functionClousures.Concat(variables),
functionAssigns.Concat(blockExpressions));
}
}
}

View File

@@ -0,0 +1,50 @@
/*
* 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 Antlr.Runtime;
using System.Linq.Expressions;
using System.Reflection.Emit;
using YATC.Scope;
namespace YATC.ASTNodes
{
class NilNode: AtomicNode
{
public NilNode(IToken payload)
: base(payload)
{
this.VmExpression = Expression.Constant(null);
}
public override void CheckSemantics(TigerScope scope, Report report)
{
this.TigerType = TigerType.Nil;
}
internal override void GenerateCode(ModuleBuilder moduleBuilder)
{
}
}
}

View File

@@ -0,0 +1,64 @@
/*
* 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 Antlr.Runtime;
using System;
using System.Linq.Expressions;
using System.Reflection.Emit;
using System.Text.RegularExpressions;
using YATC.Scope;
namespace YATC.ASTNodes
{
class StringNode : AtomicNode
{
public StringNode(IToken payload)
: base(payload)
{
string tmp = Text.Substring(1, Text.Length - 2);
tmp = Regex.Replace(tmp, @"\\(\n|\r|\t|\s)+\\", string.Empty);
tmp = Regex.Replace(tmp, @"(\\\d\d\d)", ToAscii);
Value = Regex.Unescape(tmp);
}
public string Value { get; private set; }
public override void CheckSemantics(TigerScope scope, Report report)
{
this.TigerType = TigerType.String;
}
internal override void GenerateCode(ModuleBuilder moduleBuilder)
{
this.VmExpression = Expression.Constant(this.Value);
}
private string ToAscii(Match m)
{
var a = int.Parse(m.Groups[0].Value.Substring(1));
return Convert.ToChar(a).ToString();
}
}
}

View File

@@ -0,0 +1,78 @@
/*
* 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 Antlr.Runtime;
using System.Reflection.Emit;
using YATC.Scope;
namespace YATC.ASTNodes
{
internal class VarAccessNode : AtomicNode
{
public VarAccessNode(IToken payload)
: base(payload)
{
}
public TypeNode IdNode { get { return (TypeNode)TigerChildren[0]; } }
public AccessNode AccessNode { get { return TigerChildren.Length > 1 ? (AccessNode)TigerChildren[1] : null; } }
public string Name { get { return IdNode.Text; } }
public VariableInfo VariableInfo { get; set; }
public override void CheckSemantics(TigerScope scope, Report report)
{
this.VariableInfo = scope.FindVariableInfo(this.Name, false);
if (this.VariableInfo == null)
{
report.AddError(this.Line, this.Column, "Undeclared variable: '{0}'.", this.Name);
this.TigerType = TigerType.Error;
return;
}
if (this.AccessNode != null)
{
this.AccessNode.ParentType = this.VariableInfo.Holder.TigerType;
this.AccessNode.CheckSemantics(scope, report);
this.TigerType = this.AccessNode.TigerType;
return;
}
this.TigerType = VariableInfo.Holder.TigerType;
}
internal override void GenerateCode(ModuleBuilder moduleBuilder)
{
this.VmExpression = this.VariableInfo.ParameterExpression;
if (this.AccessNode == null)
return;
this.AccessNode.VmExpression = this.VmExpression;
this.AccessNode.GenerateCode(moduleBuilder);
this.VmExpression = this.AccessNode.VmExpression;
}
}
}

View File

@@ -0,0 +1,85 @@
/*
* 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 Antlr.Runtime;
using YATC.Scope;
namespace YATC.ASTNodes
{
abstract class ArithmeticNode : BinaryNode
{
protected ArithmeticNode(IToken payload)
: base(payload)
{
}
public override void CheckSemantics(TigerScope scope, Report report)
{
base.CheckSemantics(scope, report);
bool bothOk = this.LeftOperandNode.IsOk && this.RightOperandNode.IsOk;
if (this.LeftOperandNode.IsOk)
{
if (this.LeftOperandNode.TigerType.Basetype == BaseType.Void)
{
report.AddError(this.Line, this.Column,
"Type mismatch: Invalid use of binary arithmetic operator with a non-valued left expression.");
this.TigerType = TigerType.Error;
bothOk = false;
}
else
if (this.LeftOperandNode.TigerType.Basetype != BaseType.Int)
{
report.AddError(this.Line, this.Column,
"Type mismatch: Invalid use of binary arithmetic operator with a non-int left expression: '{0}' was found.",
this.LeftOperandNode.TigerType.Name);
this.TigerType = TigerType.Error;
bothOk = false;
}
}
if (this.RightOperandNode.IsOk)
{
if (this.RightOperandNode.TigerType.Basetype == BaseType.Void)
{
report.AddError(this.Line, this.Column,
"Type mismatch: Invalid use of binary arithmetic operator with a non-valued right expression.");
this.TigerType = TigerType.Error;
bothOk = false;
}
else
if (this.RightOperandNode.TigerType.Basetype != BaseType.Int)
{
report.AddError(this.Line, this.Column,
"Type mismatch: Invalid use of binary arithmetic operator with a non-int right expression: '{0}' was found.",
this.RightOperandNode.TigerType.Name);
this.TigerType = TigerType.Error;
bothOk = false;
}
}
this.TigerType = bothOk ? TigerType.Int : TigerType.Error;
}
}
}

View File

@@ -0,0 +1,40 @@
/*
* 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.Linq.Expressions;
using Antlr.Runtime;
namespace YATC.ASTNodes
{
class DivNode : ArithmeticNode
{
public DivNode(IToken payload)
: base(payload)
{
this.ExpressionType = ExpressionType.Divide;
}
}
}

View File

@@ -0,0 +1,40 @@
/*
* 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.Linq.Expressions;
using Antlr.Runtime;
namespace YATC.ASTNodes
{
class MinusNode : ArithmeticNode
{
public MinusNode(IToken payload)
: base(payload)
{
this.ExpressionType = ExpressionType.Subtract;
}
}
}

View File

@@ -0,0 +1,40 @@
/*
* 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.Linq.Expressions;
using Antlr.Runtime;
namespace YATC.ASTNodes
{
class MultNode : ArithmeticNode
{
public MultNode(IToken payload)
: base(payload)
{
this.ExpressionType = ExpressionType.Multiply;
}
}
}

View File

@@ -0,0 +1,40 @@
/*
* 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.Linq.Expressions;
using Antlr.Runtime;
namespace YATC.ASTNodes
{
class PlusNode : ArithmeticNode
{
public PlusNode(IToken payload)
: base(payload)
{
this.ExpressionType = ExpressionType.Add;
}
}
}

View File

@@ -0,0 +1,66 @@
/*
* 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 Antlr.Runtime;
using System.Linq.Expressions;
using System.Reflection.Emit;
using YATC.Scope;
namespace YATC.ASTNodes
{
abstract class BinaryNode : ExpressionNode
{
public BinaryNode(IToken payload)
: base(payload)
{
}
protected ExpressionType ExpressionType;
public ExpressionNode LeftOperandNode { get { return (ExpressionNode)TigerChildren[0]; } }
public ExpressionNode RightOperandNode { get { return (ExpressionNode)TigerChildren[1]; } }
public override void CheckSemantics(TigerScope scope, Report report)
{
this.LeftOperandNode.CheckSemantics(scope, report);
this.RightOperandNode.CheckSemantics(scope, report);
if (!this.LeftOperandNode.IsOk || !this.RightOperandNode.IsOk)
// No se reporta el error para evitar cascadas de errores
this.TigerType = TigerType.Error;
}
internal override void GenerateCode(ModuleBuilder moduleBuilder)
{
this.LeftOperandNode.GenerateCode(moduleBuilder);
this.RightOperandNode.GenerateCode(moduleBuilder);
this.VmExpression = Expression.MakeBinary(this.ExpressionType,
this.LeftOperandNode.VmExpression,
this.RightOperandNode.VmExpression
);
}
}
}

View File

@@ -0,0 +1,40 @@
/*
* 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 Antlr.Runtime;
using System;
using System.Linq.Expressions;
namespace YATC.ASTNodes
{
class AndNode : LogicNode
{
public AndNode(IToken payload)
: base(payload)
{
this.ExpressionType = ExpressionType.AndAlso;
}
}
}

View File

@@ -0,0 +1,106 @@
/*
* 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 Antlr.Runtime;
using System.Linq.Expressions;
using System.Reflection.Emit;
using YATC.Scope;
namespace YATC.ASTNodes
{
abstract class LogicNode : BinaryNode
{
protected LogicNode(IToken payload)
: base(payload)
{
}
public override void CheckSemantics(TigerScope scope, Report report)
{
base.CheckSemantics(scope, report);
bool bothOk = this.LeftOperandNode.IsOk && this.RightOperandNode.IsOk;
if (this.LeftOperandNode.IsOk)
{
if (this.LeftOperandNode.TigerType.Basetype == BaseType.Void)
{
report.AddError(this.Line, this.Column,
"Type mismatch: Invalid use of binary logical operator with a non-valued left expression.");
this.TigerType = TigerType.Error;
bothOk = false;
}
else
if (this.LeftOperandNode.TigerType.Basetype != BaseType.Int)
{
report.AddError(this.Line, this.Column,
"Type mismatch: Invalid use of binary logical operator with a non-int left expression: '{0}' was found.",
this.LeftOperandNode.TigerType.Name);
this.TigerType = TigerType.Error;
bothOk = false;
}
}
if (this.RightOperandNode.IsOk)
{
if (this.RightOperandNode.TigerType.Basetype == BaseType.Void)
{
report.AddError(this.Line, this.Column,
"Type mismatch: Invalid use of binary logical operator with a non-valued right expression.");
this.TigerType = TigerType.Error;
bothOk = false;
}
else
if (this.RightOperandNode.TigerType.Basetype != BaseType.Int)
{
report.AddError(this.Line, this.Column,
"Type mismatch: Invalid use of binary logical operator with a non-int right expression: '{0}' was found.",
this.RightOperandNode.TigerType.Name);
this.TigerType = TigerType.Error;
bothOk = false;
}
}
this.TigerType = bothOk ? TigerType.Int : TigerType.Error;
}
internal override void GenerateCode(ModuleBuilder moduleBuilder)
{
this.LeftOperandNode.GenerateCode(moduleBuilder);
this.RightOperandNode.GenerateCode(moduleBuilder);
this.LeftOperandNode.VmExpression = Expression.NotEqual(this.LeftOperandNode.VmExpression,
Expression.Constant(0));
this.RightOperandNode.VmExpression = Expression.NotEqual(this.RightOperandNode.VmExpression,
Expression.Constant(0));
this.VmExpression = Expression.MakeBinary(this.ExpressionType,
this.LeftOperandNode.VmExpression,
this.RightOperandNode.VmExpression
);
this.VmExpression = Expression.Condition(this.VmExpression, Expression.Constant(1), Expression.Constant(0));
}
}
}

View File

@@ -0,0 +1,40 @@
/*
* 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.Linq.Expressions;
using Antlr.Runtime;
namespace YATC.ASTNodes
{
class OrNode : LogicNode
{
public OrNode(IToken payload)
: base(payload)
{
this.ExpressionType = ExpressionType.OrElse;
}
}
}

View File

@@ -0,0 +1,40 @@
/*
* 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 Antlr.Runtime;
using System;
using System.Linq.Expressions;
namespace YATC.ASTNodes
{
class EqNode : IdentityNode
{
public EqNode(IToken payload)
: base(payload)
{
this.ExpressionType = ExpressionType.Equal;
}
}
}

View File

@@ -0,0 +1,83 @@
/*
* 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 Antlr.Runtime;
using YATC.Scope;
namespace YATC.ASTNodes
{
abstract class IdentityNode : RelationalNode
{
protected IdentityNode(IToken payload)
: base(payload)
{
}
public override void CheckSemantics(TigerScope scope, Report report)
{
base.CheckSemantics(scope, report);
bool bothOk = this.LeftOperandNode.IsOk && this.RightOperandNode.IsOk;
if (LeftOperandNode.IsOk && this.LeftOperandNode.TigerType.Basetype == BaseType.Void)
{
report.AddError(this.Line, this.Column,
"Type mismatch: Invalid use of binary identity operator with a non-valued left expression.");
this.TigerType = TigerType.Error;
bothOk = false;
}
if (this.RightOperandNode.IsOk && this.RightOperandNode.TigerType.Basetype == BaseType.Void)
{
report.AddError(this.Line, this.Column,
"Type mismatch: Invalid use of binary identity operator with a non-valued right expression.");
this.TigerType = TigerType.Error;
bothOk = false;
}
if (this.LeftOperandNode.TigerType.Basetype == BaseType.Nil &&
this.RightOperandNode.TigerType.Basetype == BaseType.Nil)
{
report.AddError(this.Line, this.Column,
"Type mismatch: Invalid use of binary identity operator with two nils.");
this.TigerType = TigerType.Error;
bothOk = false;
}
if (bothOk &&
!LeftOperandNode.TigerType.IsAssignableTo(RightOperandNode.TigerType) &&
!RightOperandNode.TigerType.IsAssignableTo(LeftOperandNode.TigerType))
{
report.AddError(this.Line, this.Column,
"Type mismatch: Invalid use of binary identity operator with different types: '{0}' and '{1}' were found.",
this.LeftOperandNode.TigerType.Name,
this.RightOperandNode.TigerType.Name);
this.TigerType = TigerType.Error;
bothOk = false;
}
this.TigerType = bothOk ? TigerType.Int : TigerType.Error;
}
}
}

View File

@@ -0,0 +1,40 @@
/*
* 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 Antlr.Runtime;
using System;
using System.Linq.Expressions;
namespace YATC.ASTNodes
{
class NoteqNode : IdentityNode
{
public NoteqNode(IToken payload)
: base(payload)
{
this.ExpressionType = ExpressionType.NotEqual;
}
}
}

View File

@@ -0,0 +1,42 @@
/*
* 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 Antlr.Runtime;
using System.Linq.Expressions;
namespace YATC.ASTNodes
{
/// <summary>
/// Mayor o igual que ">="
/// </summary>
class GteqNode : OrderNode
{
public GteqNode(IToken payload)
: base(payload)
{
this.ExpressionType = ExpressionType.GreaterThanOrEqual;
}
}
}

View File

@@ -0,0 +1,42 @@
/*
* 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 Antlr.Runtime;
using System.Linq.Expressions;
namespace YATC.ASTNodes
{
/// <summary>
/// Mayor que ">"
/// </summary>
class GtNode : OrderNode
{
public GtNode(IToken payload)
: base(payload)
{
this.ExpressionType = ExpressionType.GreaterThan;
}
}
}

View File

@@ -0,0 +1,42 @@
/*
* 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 Antlr.Runtime;
using System.Linq.Expressions;
namespace YATC.ASTNodes
{
/// <summary>
/// Menor o igual "<="
/// </summary>
class LteqNode : OrderNode
{
public LteqNode(IToken payload)
: base(payload)
{
this.ExpressionType = ExpressionType.LessThanOrEqual;
}
}
}

View File

@@ -0,0 +1,42 @@
/*
* 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 Antlr.Runtime;
using System.Linq.Expressions;
namespace YATC.ASTNodes
{
/// <summary>
/// Menor que "<"
/// </summary>
class LtNode : OrderNode
{
public LtNode(IToken payload)
: base(payload)
{
this.ExpressionType = ExpressionType.LessThan;
}
}
}

View File

@@ -0,0 +1,153 @@
/*
* 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.Linq.Expressions;
using System.Reflection;
using System.Reflection.Emit;
using Antlr.Runtime;
using YATC.Scope;
namespace YATC.ASTNodes
{
abstract class OrderNode : RelationalNode
{
protected OrderNode(IToken payload)
: base(payload)
{
}
public override void CheckSemantics(TigerScope scope, Report report)
{
base.CheckSemantics(scope, report);
bool bothOk = this.LeftOperandNode.IsOk && this.RightOperandNode.IsOk;
if (this.LeftOperandNode.IsOk)
{
if (this.LeftOperandNode.TigerType.Basetype == BaseType.Void)
{
report.AddError(this.Line, this.Column,
"Type mismatch: Invalid use of binary order operator with a non-valued left expression.");
this.TigerType = TigerType.Error;
bothOk = false;
}
else
if (this.LeftOperandNode.TigerType.Basetype != BaseType.Int &&
this.LeftOperandNode.TigerType.Basetype != BaseType.String)
{
report.AddError(this.Line, this.Column,
"Type mismatch: Invalid use of binary order operator with a non-int or non-string left expression: '{0}' was found.",
this.LeftOperandNode.TigerType.Name);
this.TigerType = TigerType.Error;
bothOk = false;
}
}
if (this.RightOperandNode.IsOk)
{
if (this.RightOperandNode.TigerType.Basetype == BaseType.Void)
{
report.AddError(this.Line, this.Column,
"Type mismatch: Invalid use of binary order operator with a non-valued right expression.");
this.TigerType = TigerType.Error;
bothOk = false;
}
else
if (this.RightOperandNode.TigerType.Basetype != BaseType.Int &&
this.RightOperandNode.TigerType.Basetype != BaseType.String)
{
report.AddError(this.Line, this.Column,
"Type mismatch: Invalid use of binary order operator with a non-int or non-string right expression: '{0}' was found.",
this.RightOperandNode.TigerType.Name);
this.TigerType = TigerType.Error;
bothOk = false;
}
}
if (bothOk && this.LeftOperandNode.TigerType.Basetype != this.RightOperandNode.TigerType.Basetype)
{
report.AddError(this.Line, this.Column,
"Type mismatch: Invalid use of binary order operator with string and int expressions.");
this.TigerType = TigerType.Error;
return;
}
this.TigerType = bothOk ? TigerType.Int : TigerType.Error;
}
internal override void GenerateCode(ModuleBuilder moduleBuilder)
{
this.LeftOperandNode.GenerateCode(moduleBuilder);
this.RightOperandNode.GenerateCode(moduleBuilder);
if (this.LeftOperandNode.TigerType.Basetype == BaseType.String &&
this.RightOperandNode.TigerType.Basetype == BaseType.String)
{
MethodInfo compareString = ((Func<string, string, int>)String.Compare).Method;
ParameterExpression value = Expression.Parameter(typeof (int));
Expression compareCall = Expression.Call(compareString,
this.LeftOperandNode.VmExpression,
this.RightOperandNode.VmExpression);
Expression condition = null;
switch (this.ExpressionType)
{
case ExpressionType.GreaterThan:
condition = Expression.GreaterThan(value, Expression.Constant(0));
break;
case ExpressionType.GreaterThanOrEqual:
condition = Expression.GreaterThanOrEqual(value, Expression.Constant(0));
break;
case ExpressionType.LessThan:
condition = Expression.LessThan(value, Expression.Constant(0));
break;
case ExpressionType.LessThanOrEqual:
condition = Expression.LessThanOrEqual(value, Expression.Constant(0));
break;
}
Expression block = Expression.Block(
new ParameterExpression[] {value},
new Expression[]
{
Expression.Assign(value, compareCall),
Expression.Condition(condition, Expression.Constant(1), Expression.Constant(0))
}
);
this.VmExpression = block;
}
else
{
Expression opHolds = Expression.MakeBinary(this.ExpressionType,
this.LeftOperandNode.VmExpression,
this.RightOperandNode.VmExpression);
this.VmExpression = Expression.Condition(opHolds, Expression.Constant(1), Expression.Constant(0));
}
}
}
}

View File

@@ -0,0 +1,46 @@
/*
* 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 Antlr.Runtime;
using System.Linq.Expressions;
using System.Reflection.Emit;
namespace YATC.ASTNodes
{
abstract class RelationalNode : BinaryNode
{
protected RelationalNode(IToken payload)
: base(payload)
{
}
internal override void GenerateCode(ModuleBuilder moduleBuilder)
{
base.GenerateCode(moduleBuilder);
this.VmExpression = Expression.Condition(this.VmExpression, Expression.Constant(1), Expression.Constant(0));
}
}
}

View File

@@ -0,0 +1,54 @@
/*
* 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 Antlr.Runtime;
using YATC.Scope;
namespace YATC.ASTNodes
{
public abstract class ExpressionNode : LocalNode
{
protected ExpressionNode(IToken payload)
: base(payload)
{
}
private TigerType _tigerType = TigerType.Error;
/// <summary>
/// Tipo resultante de la expresión
/// </summary>
public TigerType TigerType
{
get { return _tigerType; }
protected set { _tigerType = value; }
}
/// <summary>
/// Dice si un nodo está o no bien
/// </summary>
public bool IsOk { get { return !TigerType.Equals(TigerType.Error); } }
}
}

View File

@@ -0,0 +1,77 @@
/*
* 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 Antlr.Runtime;
using System.Linq.Expressions;
using System.Reflection.Emit;
using YATC.Scope;
namespace YATC.ASTNodes
{
/// <summary>
/// Permite poner números negativos
/// </summary>
class NegNode : ExpressionNode
{
public NegNode(IToken payload)
: base(payload)
{
}
protected ExpressionNode OperandNode { get { return (ExpressionNode)TigerChildren[0]; } }
public override void CheckSemantics(TigerScope scope, Report report)
{
this.OperandNode.CheckSemantics(scope, report);
if (!this.OperandNode.IsOk)
{
this.TigerType = TigerType.Error;
return;
}
if (OperandNode.TigerType.Basetype == BaseType.Void)
{
this.TigerType = TigerType.Error;
report.AddError(this.Line, this.Column, "Type mismatch: Invalid use of unary minus operator with a non-valued expression.");
return;
}
if (OperandNode.TigerType.Basetype != BaseType.Int)
{
this.TigerType = TigerType.Error;
report.AddError(this.Line, this.Column, "Type mismatch: Invalid use of unary minus operator with a non-int expression.");
return;
}
this.TigerType = TigerType.Int;
}
internal override void GenerateCode(ModuleBuilder moduleBuilder)
{
this.OperandNode.GenerateCode(moduleBuilder);
this.VmExpression = Expression.Negate(this.OperandNode.VmExpression);
}
}
}

View File

@@ -0,0 +1,154 @@
/*
* 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 Antlr.Runtime;
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection.Emit;
using YATC.Scope;
namespace YATC.ASTNodes
{
class RecordInstNode : ExpressionNode
{
public RecordInstNode(IToken payload)
: base(payload)
{
}
public TypeNode IdNode { get { return (TypeNode)TigerChildren[0]; } }
public FieldInstNode[] FieldInstNodes
{
get
{
return TigerChildren.Length > 1 ?
TigerChildren[1].TigerChildren.Cast<FieldInstNode>().ToArray() :
new FieldInstNode[] {};
}
}
private RecordType _recordType;
public override void CheckSemantics(TigerScope scope, Report report)
{
TigerTypeInfo recordInfo = scope.FindTypeInfo(this.IdNode.Name, false);
if (recordInfo == null)
{
report.AddError(this.Line, this.Column,
"Undeclared record type: '{0}'.", this.IdNode.Name);
this.TigerType = TigerType.Error;
return;
}
if (recordInfo.Holder.TigerType.Basetype != BaseType.Record)
{
report.AddError(this.Line, this.Column,
"Type mismatch: given type is not a record: '{0}'.", this.IdNode.Name);
this.TigerType = TigerType.Error;
return;
}
_recordType = recordInfo.Holder.TigerType as RecordType;
if (_recordType == null)
throw new NullReferenceException();
if (_recordType.FieldInfos.Length != this.FieldInstNodes.Length)
{
report.AddError(this.Line, this.Column,
"Record fields length mismatch '{0}': expecting {1} and {2} found.",
this.IdNode.Name,
_recordType.FieldInfos.Length,
this.FieldInstNodes.Length);
this.TigerType = TigerType.Error;
return;
}
for (int i = 0; i < this.FieldInstNodes.Length; i++)
{
FieldInstNode field = this.FieldInstNodes[i];
if (field.IdNode.Name != _recordType.FieldInfos[i].Name)
{
report.AddError(this.Line, this.Column,
"Field name mismatch: field number {0} of type '{1}' should be called '{2}' instead of '{3}'.",
i.ToString(),
this.IdNode.Name,
_recordType.FieldInfos[i].Name,
field.IdNode.Name);
this.TigerType = TigerType.Error;
return;
}
field.ExpressionNode.CheckSemantics(scope, report);
if (!field.ExpressionNode.IsOk)
{
this.TigerType = TigerType.Error;
return;
}
if (!field.ExpressionNode.TigerType.IsAssignableTo(_recordType.FieldInfos[i].Holder.TigerType))
{
report.AddError(this.Line, this.Column,
"Type mismatch: field '{1}' of type '{0}' should be of type '{2}' instead of '{3}'",
this.IdNode.Name, field.IdNode.Name,
_recordType.FieldInfos[i].Holder.TigerType.Name, field.ExpressionNode.TigerType.Name);
this.TigerType = TigerType.Error;
return;
}
}
this.TigerType = _recordType;
}
internal override void GenerateCode(ModuleBuilder moduleBuilder)
{
NewExpression ctor = Expression.New(this.TigerType.GetCLRType());
ParameterExpression tmp = Expression.Parameter(this.TigerType.GetCLRType());
var fieldBindings = new MemberBinding[_recordType.FieldInfos.Length];
for (int i = 0; i < FieldInstNodes.Length; i++)
{
this.FieldInstNodes[i].ExpressionNode.GenerateCode(moduleBuilder);
fieldBindings[i] = Expression.Bind(
_recordType.GetCLRType().GetMember(_recordType.FieldInfos[i].Name)[0],
Expression.Convert(
this.FieldInstNodes[i].ExpressionNode.VmExpression,
_recordType.FieldInfos[i].Holder.TigerType.GetCLRType()
)
);
}
Expression initializer = Expression.MemberInit(ctor, fieldBindings);
var assign = Expression.Assign(tmp, initializer);
BlockExpression initBlockExpression = Expression.Block(
new ParameterExpression[] { tmp },
new Expression[] { assign, tmp });
this.VmExpression = initBlockExpression;
}
}
}

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 Antlr.Runtime;
using System.Linq.Expressions;
using System.Reflection.Emit;
using YATC.Scope;
namespace YATC.ASTNodes
{
class AssignNode : StatementNode
{
public AssignNode(IToken payload)
: base(payload)
{
}
public VarAccessNode LeftValueNode { get { return (VarAccessNode)TigerChildren[0]; } }
public ExpressionNode RightExpressionNode { get { return (ExpressionNode)TigerChildren[1]; } }
public override void CheckSemantics(TigerScope scope, Report report)
{
this.LeftValueNode.CheckSemantics(scope, report);
this.RightExpressionNode.CheckSemantics(scope, report);
// No se reporta el error para evitar cascadas de errores
this.TigerType = this.LeftValueNode.IsOk && this.RightExpressionNode.IsOk
? TigerType.Void
: TigerType.Error;
if (!this.IsOk)
return;
if (this.RightExpressionNode.TigerType.Equals(TigerType.Void))
{
report.AddError(this.Line, this.Column, "Right hand side must evaluate to a returning value.");
this.TigerType = TigerType.Error;
return;
}
/* Assignment */
if (!this.RightExpressionNode.TigerType.IsAssignableTo(this.LeftValueNode.TigerType))
{
report.AddError(this.Line, this.Column,
"Type mismatch: Types of variable declaration and expression do not match: '{0}' and '{1}'",
this.LeftValueNode.TigerType.Name, this.RightExpressionNode.TigerType.Name);
this.TigerType = TigerType.Error;
return;
}
// checks if left variable is not read-only, i.e., it is not defined in a ForNode
// la segunda pregunta accessNode == null es por si se quiere agregar campos o arrays
if (this.LeftValueNode.VariableInfo.IsReadOnly && this.LeftValueNode.AccessNode == null)
{
this.TigerType = TigerType.Error;
report.AddError(this.Line, this.Column,
"Cannot assign to a read-only variable (it may be declared within a for control structure).");
return;
}
this.TigerType = TigerType.Void;
}
internal override void GenerateCode(ModuleBuilder moduleBuilder)
{
this.LeftValueNode.GenerateCode(moduleBuilder);
this.RightExpressionNode.GenerateCode(moduleBuilder);
//int isOK = (int)(34 < 342);
this.VmExpression =
Expression.Block(
Expression.Assign(
this.LeftValueNode.VmExpression,
Expression.Convert(
this.RightExpressionNode.VmExpression,
this.LeftValueNode.TigerType.GetCLRType()
)
),
Expression.Empty());
}
}
}

View File

@@ -0,0 +1,80 @@
/*
* 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 Antlr.Runtime;
using System.Linq.Expressions;
using System.Reflection.Emit;
using YATC.Scope;
namespace YATC.ASTNodes
{
class BreakNode : StatementNode
{
public BreakNode(IToken payload)
: base(payload)
{
}
public IBreakeableNode Owner { get; set; }
public override void CheckSemantics(TigerScope scope, Report report)
{
foreach (var node in this.GetNodesToRoot())
{
var exprSeq = node as ExprSeqNode;
if (exprSeq != null)
exprSeq.HasBreakInside = true;
var breakable = node as IBreakeableNode;
if (breakable != null)
{
this.Owner = breakable;
break;
}
if (node is FunDeclNode)
{
report.AddError(this.Line, this.Column,
"Break loop control structure not found within function.");
break;
}
}
if (this.Owner == null)
{
report.AddError(Line, Column, "Break does not have a matching loop control structure owner.");
this.TigerType = TigerType.Error;
return;
}
this.TigerType = TigerType.Void;
}
internal override void GenerateCode(ModuleBuilder moduleBuilder)
{
this.VmExpression = Expression.Break(this.Owner.BreakTarget);
}
}
}

View File

@@ -0,0 +1,49 @@
/*
* 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 Antlr.Runtime;
using System.Reflection.Emit;
using YATC.Scope;
namespace YATC.ASTNodes
{
class EmptyNode : StatementNode
{
public EmptyNode(IToken payload)
: base(payload)
{
}
public override void CheckSemantics(TigerScope scope, Report report)
{
this.TigerType = TigerType.Void;
}
internal override void GenerateCode(ModuleBuilder moduleBuilder)
{
// vacío a propósito
}
}
}

View File

@@ -0,0 +1,127 @@
/*
* 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 Antlr.Runtime;
using System.Linq.Expressions;
using System.Reflection.Emit;
using YATC.Scope;
namespace YATC.ASTNodes
{
/// <summary>
/// for id := expr_1 to expr_2 do expr_3;
/// </summary>
class ForNode : StatementNode, IBreakeableNode
{
public ForNode(IToken payload)
: base(payload)
{
}
public TypeNode IdNode { get { return (TypeNode)TigerChildren[0]; } }
public ExpressionNode FromExpression { get { return (ExpressionNode)TigerChildren[1]; } }
public ExpressionNode ToExpression { get { return (ExpressionNode)TigerChildren[2]; } }
public ExpressionNode DoExpression { get { return (ExpressionNode)TigerChildren[3]; } }
public LabelTarget BreakTarget { get; set; }
private VariableInfo _iterVarInfo;
public override void CheckSemantics(TigerScope scope, Report report)
{
this.FromExpression.CheckSemantics(scope, report);
this.ToExpression.CheckSemantics(scope, report);
TigerScope innerScope = scope.CreateChildScope();
if (innerScope.CanFindFunVarInfo(this.IdNode.Name, false))
report.AddWarning(this.Line, this.Column,
"Variable name hides outer scope variable or function: '{0}'.",
this.IdNode.Name);
_iterVarInfo = new VariableInfo(this.IdNode.Name, new TigerTypeHolder(TigerType.Int), false) { IsReadOnly = true };
innerScope.Add(_iterVarInfo);
this.DoExpression.CheckSemantics(innerScope, report);
if (!this.FromExpression.IsOk || !this.ToExpression.IsOk || !this.DoExpression.IsOk)
{
this.TigerType = TigerType.Error;
return;
}
if (this.FromExpression.TigerType.Basetype != BaseType.Int ||
this.ToExpression.TigerType.Basetype != BaseType.Int)
{
this.TigerType = TigerType.Error;
report.AddError(this.Line, this.Column,
"Type mismatch: Expecting integer (or alias) type on intervals expression: '{0}' and '{1}' were found.",
this.FromExpression.TigerType.Name,
this.ToExpression.TigerType.Name);
return;
}
if (this.DoExpression.TigerType.Basetype != BaseType.Void)
{
this.TigerType = TigerType.Error;
report.AddError(this.Line, this.Column,
"Type mismatch: Expecting void return type in for expression: '{0}' was found.",
this.DoExpression.TigerType.Name);
return;
}
this.TigerType = TigerType.Void;
}
internal override void GenerateCode(ModuleBuilder moduleBuilder)
{
BreakTarget = Expression.Label();
ParameterExpression iter = _iterVarInfo.ParameterExpression =
Expression.Parameter(typeof(int), "iter");
ParameterExpression fromExpr = Expression.Parameter(typeof(int), "fromExpr");
ParameterExpression toExpr = Expression.Parameter(typeof(int), "toExpr");
this.FromExpression.GenerateCode(moduleBuilder);
this.ToExpression.GenerateCode(moduleBuilder);
this.DoExpression.GenerateCode(moduleBuilder);
BlockExpression blockExpression = Expression.Block(
new ParameterExpression[] { iter, fromExpr, toExpr },
new Expression[]
{
Expression.Assign(fromExpr, this.FromExpression.VmExpression),
Expression.Assign(toExpr, this.ToExpression.VmExpression),
Expression.Assign(iter, fromExpr),
Expression.Loop(Expression.Block(
Expression.IfThen(Expression.GreaterThan(iter, toExpr),
Expression.Break(BreakTarget)),
this.DoExpression.VmExpression,
Expression.PostIncrementAssign(iter)
), BreakTarget)
});
this.VmExpression = blockExpression;
}
}
}

View File

@@ -0,0 +1,34 @@
/*
* 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.ASTNodes
{
interface IBreakeableNode
{
LabelTarget BreakTarget { get; set; }
}
}

View File

@@ -0,0 +1,37 @@
/*
* 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 Antlr.Runtime;
namespace YATC.ASTNodes
{
abstract class StatementNode : ExpressionNode
{
protected StatementNode(IToken payload)
: base(payload)
{
}
}
}

View File

@@ -0,0 +1,94 @@
/*
* 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 Antlr.Runtime;
using System.Linq.Expressions;
using System.Reflection.Emit;
using YATC.Scope;
namespace YATC.ASTNodes
{
class WhileNode : StatementNode, IBreakeableNode
{
public WhileNode(IToken payload)
: base(payload)
{
}
public LabelTarget BreakTarget { get; set; }
public ExpressionNode ConditionExpression { get { return (ExpressionNode)TigerChildren[0]; } }
public ExpressionNode DoExpression { get { return (ExpressionNode)TigerChildren[1]; } }
public override void CheckSemantics(TigerScope scope, Report report)
{
this.ConditionExpression.CheckSemantics(scope, report);
this.DoExpression.CheckSemantics(scope, report);
if (!this.ConditionExpression.IsOk || !this.DoExpression.IsOk)
{
this.TigerType = TigerType.Error;
return;
}
if (this.ConditionExpression.TigerType.Basetype != BaseType.Int)
{
report.AddError(Line, Column,
"Type mismatch: Expecting integer (or alias) type on condition expression: '{0}' was found.",
this.ConditionExpression.TigerType.Name);
this.TigerType = TigerType.Error;
return;
}
if (this.DoExpression.TigerType.Basetype != BaseType.Void)
{
this.TigerType = TigerType.Error;
report.AddError(this.Line, this.Column,
"Type mismatch: Expecting void return type in while expression: '{0}' was found.",
this.DoExpression.TigerType.Name);
return;
}
this.TigerType = TigerType.Void;
}
internal override void GenerateCode(ModuleBuilder moduleBuilder)
{
BreakTarget = Expression.Label();
this.ConditionExpression.GenerateCode(moduleBuilder);
this.DoExpression.GenerateCode(moduleBuilder);
ConditionalExpression conditionalExpression = Expression.IfThenElse(
Expression.NotEqual(this.ConditionExpression.VmExpression, Expression.Constant(0)),
this.DoExpression.VmExpression,
Expression.Break(this.BreakTarget));
this.VmExpression = Expression.Loop(conditionalExpression, this.BreakTarget);
}
}
}

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.
*
*/
using Antlr.Runtime;
using YATC.Scope;
namespace YATC.ASTNodes
{
internal class FillInTypeNode : TypeNode
{
public FillInTypeNode(IToken payload)
: base(payload)
{
}
public override void CheckSemantics(TigerScope scope, Report report)
{
this.TigerType = TigerType.FillIn;
}
}
}

View File

@@ -0,0 +1,44 @@
/*
* 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 Antlr.Runtime;
using YATC.Scope;
namespace YATC.ASTNodes
{
class IdNode : TypeNode
{
public IdNode(IToken payload)
: base(payload)
{
}
public override void CheckSemantics(TigerScope scope, Report report)
{
// vacio exprofeso
this.TigerType = TigerType.Unknown;
}
}
}

View File

@@ -0,0 +1,69 @@
/*
* 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 Antlr.Runtime;
using System;
using System.Linq.Expressions;
using System.Reflection.Emit;
using YATC.Scope;
namespace YATC.ASTNodes
{
class TypeNode : ExpressionNode
{
public TypeNode(IToken payload)
: base(payload)
{
}
public string Name { get { return Text; } }
public override void CheckSemantics(TigerScope scope, Report report)
{
switch (Text)
{
case "int":
TigerType = TigerType.Int;
break;
case "string":
TigerType = TigerType.String;
break;
case "nil":
TigerType = TigerType.Nil;
break;
default:
throw new ArgumentOutOfRangeException();
}
}
internal override void GenerateCode(ModuleBuilder moduleBuilder)
{
Type retType = this.TigerType.GetCLRType();
this.VmExpression = retType == null
? (Expression)Expression.Constant(null)
: Expression.Parameter(retType);
}
}
}

View File

@@ -0,0 +1,42 @@
/*
* 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 YATC.Scope;
namespace YATC.ASTNodes
{
internal class VoidNode : TypeNode
{
public VoidNode()
: base(null)
{
}
public override void CheckSemantics(TigerScope scope, Report report)
{
this.TigerType = TigerType.Void;
}
}
}

View File

@@ -0,0 +1,46 @@
/*
* 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.Reflection.Emit;
using Antlr.Runtime;
using System.Linq.Expressions;
using YATC.Scope;
namespace YATC.ASTNodes
{
public abstract class LocalNode : TigerNode
{
protected LocalNode(IToken payload)
: base(payload)
{
}
public abstract void CheckSemantics(TigerScope scope, Report report);
internal abstract void GenerateCode(ModuleBuilder moduleBuilder);
public Expression VmExpression;
}
}

View File

@@ -0,0 +1,221 @@
/*
* 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 Antlr.Runtime;
using System;
using System.IO;
using System.Linq.Expressions;
using System.Reflection;
using System.Reflection.Emit;
using YATC.Scope;
namespace YATC.ASTNodes
{
public class ProgramNode : TigerNode
{
public ProgramNode(IToken payload)
: base(payload)
{
Report = new Report();
Scope = new TigerScope();
RecordNumber = 0;
AddStdLib();
}
public readonly Report Report;
internal readonly TigerScope Scope;
internal static int RecordNumber;
private FunctionInfo _print;
private FunctionInfo _printLine;
private FunctionInfo _printi;
private FunctionInfo _printiline;
private FunctionInfo _getline;
private FunctionInfo _ord;
private FunctionInfo _chr;
private FunctionInfo _exit;
private FunctionInfo _not;
private FunctionInfo _concat;
private FunctionInfo _substring;
private FunctionInfo _size;
internal ExpressionNode ExpressionNode { get { return (ExpressionNode)GetChild(0); } }
public void CheckSemantics()
{
if (this.ExpressionNode != null)
this.ExpressionNode.CheckSemantics(Scope, Report);
}
public AssemblyBuilder GenerateCode(string name, string fileName, string outputDir)
{
AssemblyName assemblyName = new AssemblyName(name);
AssemblyBuilder assembly = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName,
AssemblyBuilderAccess.RunAndSave,
outputDir);
ModuleBuilder moduleBuilder = assembly.DefineDynamicModule(fileName, fileName);
TypeBuilder typeBuilder = moduleBuilder.DefineType("yatcProgram");
MethodBuilder mainMethod = typeBuilder.DefineMethod("main", MethodAttributes.Static);
Expression mainBlock;
if (this.ExpressionNode != null)
{
this.ExpressionNode.GenerateCode(moduleBuilder);
ParameterExpression outcode = Expression.Parameter(typeof(int));
ParameterExpression exception = Expression.Parameter(typeof(Exception));
MemberInfo excMessageMember = typeof(Exception).GetMember("Message")[0];
MemberInfo consoleError = typeof(Console).GetMember("Error")[0];
MethodInfo errorWrite = typeof(TextWriter).GetMethod("WriteLine", new Type[] { typeof(string) });
mainBlock = Expression.Block(
new ParameterExpression[] { outcode, exception },
new Expression[]
{
Expression.Assign(outcode, Expression.Constant(0)),
Expression.TryCatch(
Expression.Block(
this.ExpressionNode.VmExpression,
Expression.Empty()
),
new CatchBlock[]
{
Expression.MakeCatchBlock(
typeof(Exception),
exception,
Expression.Block(
Expression.Call(
Expression.MakeMemberAccess(null, consoleError),
errorWrite,
Expression.MakeMemberAccess(exception, excMessageMember)
),
Expression.Assign(outcode, Expression.Constant(1)),
Expression.Empty()
),
Expression.Constant(true)
)
}
),
outcode
});
}
else
{
mainBlock = Expression.Constant(0);
}
LambdaExpression lambdaMainBlock = Expression.Lambda<Func<int>>(mainBlock);
lambdaMainBlock.CompileToMethod(mainMethod);
assembly.SetEntryPoint(mainMethod);
typeBuilder.CreateType();
moduleBuilder.CreateGlobalFunctions();
return assembly;
}
private void AddStdLib()
{
var stringHolder = new TigerTypeHolder(TigerType.String);
var intHolder = new TigerTypeHolder(TigerType.Int);
var voidHolder = new TigerTypeHolder(TigerType.Void);
_print = new FunctionInfo("print", new[] { new VariableInfo("s", stringHolder, true), }, voidHolder, true);
_printLine = new FunctionInfo("printline", new[] { new VariableInfo("s", stringHolder, true), }, voidHolder, true);
_printi = new FunctionInfo("printi", new[] { new VariableInfo("i", intHolder, true), }, voidHolder, true);
_printiline = new FunctionInfo("printiline", new[] { new VariableInfo("i", intHolder, true), }, voidHolder, true);
_getline = new FunctionInfo("getline", new VariableInfo[0], stringHolder, true);
_ord = new FunctionInfo("ord", new[] { new VariableInfo("s", stringHolder, true), }, intHolder, true);
_chr = new FunctionInfo("chr", new[] { new VariableInfo("i", intHolder, true), }, stringHolder, true);
_size = new FunctionInfo("size", new[] { new VariableInfo("s", stringHolder, true), }, intHolder, true);
_substring = new FunctionInfo("substring", new[]
{
new VariableInfo("s", stringHolder, true),
new VariableInfo("f", intHolder, true),
new VariableInfo("n", intHolder, true),
},
stringHolder, true);
_concat = new FunctionInfo("concat",
new[]
{
new VariableInfo("s1", stringHolder, true),
new VariableInfo("s2", stringHolder, true),
},
stringHolder,
true);
_not = new FunctionInfo("not", new[] { new VariableInfo("i", intHolder, true), }, intHolder, true);
_exit = new FunctionInfo("exit", new[] { new VariableInfo("i", intHolder, true), }, voidHolder, true);
Scope.Add(_print);
Scope.Add(_printLine);
Scope.Add(_printi);
Scope.Add(_printiline);
Scope.Add(_getline);
Scope.Add(_ord);
Scope.Add(_chr);
Scope.Add(_size);
Scope.Add(_substring);
Scope.Add(_concat);
Scope.Add(_not);
Scope.Add(_exit);
Scope.Add(new TigerTypeInfo("string", new TigerTypeHolder(TigerType.String), true));
Scope.Add(new TigerTypeInfo("int", new TigerTypeHolder(TigerType.Int), true));
Scope.Add(new TigerTypeInfo("nil", new TigerTypeHolder(TigerType.Nil), true));
// althought it is defined and can be returned as a valid program expression it cannot be called by name
Scope.Add(new TigerTypeInfo("!void", new TigerTypeHolder(TigerType.Void), true));
MethodInfo writeString = ((Action<string>)Console.Write).Method;
MethodInfo writeLineString = ((Action<string>)Console.WriteLine).Method;
MethodInfo writeInt = ((Action<int>)Console.Write).Method;
MethodInfo writeLineInt = ((Action<int>)Console.WriteLine).Method;
MethodInfo readLine = ((Func<string>)Console.ReadLine).Method;
MethodInfo chr = ((Func<byte, string>)Convert.ToString).Method;
MethodInfo concat = ((Func<string, string, string>)string.Concat).Method;
_print.MethodInfo = writeString;
_printLine.MethodInfo = writeLineString;
_printi.MethodInfo = writeInt;
_printiline.MethodInfo = writeLineInt;
_getline.MethodInfo = readLine;
//_chr.MethodInfo = chr;
_concat.MethodInfo = concat;
Expression<Func<int, string>> lambdaChr = (b) => ( new string(Convert.ToChar(((byte)(b) > 127) ? int.MaxValue : (byte)b), 1));
Expression<Func<string, int>> lambdaOrd = (x) => (string.IsNullOrEmpty(x) ? -1 : Convert.ToByte(x[0]));
Expression<Func<string, int>> lambdaSize = (x) => (x.Length);
Expression<Func<string, int, int, string>> lambdaSubstring = (x, y, z) => (x.Substring(y, z));
Expression<Func<int, int>> lambdaNot = (x) => (x == 0 ? 1 : 0);
_ord.LambdaExpression = lambdaOrd;
_size.LambdaExpression = lambdaSize;
_substring.LambdaExpression = lambdaSubstring;
_not.LambdaExpression = lambdaNot;
_chr.LambdaExpression = lambdaChr;
}
}
}

View File

@@ -0,0 +1,77 @@
/*
* 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 Antlr.Runtime;
using Antlr.Runtime.Tree;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace YATC.Scope
{
public abstract class TigerNode : CommonTree
{
protected TigerNode(IToken payload)
: base(payload)
{
}
public bool IsRoot
{
get { return Parent == null; }
}
/// <summary>
/// Lists all the nodes from current to root
/// </summary>
public IEnumerable<TigerNode> GetNodesToRoot()
{
if (this.IsRoot)
yield break;
Debug.Assert(this.Parent is TigerNode);
var parent = Parent as TigerNode;
yield return parent;
foreach (var item in parent.GetNodesToRoot())
yield return item;
}
public int Column { get { return CharPositionInLine; } }
//public new TigerNode Parent
//{
// get { return (TigerNode)base.Parent; }
//}
public TigerNode[] TigerChildren
{
get
{
return base.Children == null ? new TigerNode[0] : base.Children.Cast<TigerNode>().ToArray();
}
}
}
}

View File

@@ -0,0 +1,83 @@
/*
* 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 Antlr.Runtime;
using Antlr.Runtime.Tree;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using YATC.Grammar;
namespace YATC.ASTNodes
{
public class TigerTreeAdaptor : CommonTreeAdaptor
{
private readonly Dictionary<int, Type> _payloadCache;
public TigerTreeAdaptor()
{
_payloadCache = new Dictionary<int, Type>
{
{tigerParser.INTKEY, typeof (TypeNode)},
{tigerParser.STRINGKEY, typeof (TypeNode)},
{tigerParser.NILKEY, typeof (TypeNode)}
};
FieldInfo[] _fields = typeof(tigerParser).GetFields();
Assembly executingAssembly = Assembly.GetExecutingAssembly();
foreach (var field in _fields)
{
if (!field.IsStatic)
continue;
string name = GetName(field.Name);
Type type = executingAssembly.GetType(name);
if (type != null)
_payloadCache[(int)field.GetRawConstantValue()] = type;
}
}
private string GetName(string name)
{
var sb = new StringBuilder();
foreach (var x in name.Split('_'))
sb.Append(char.ToUpper(x[0]) + x.Substring(1, x.Length - 1).ToLower());
return string.Format("YATC.ASTNodes.{0}Node", sb);
}
public override object Create(IToken payload)
{
if (payload == null)
return new UnknownNode(null);
//return base.Create(null);
Type type;
bool foundType = _payloadCache.TryGetValue(payload.Type, out type);
return foundType ? Activator.CreateInstance(type, payload) : new UnknownNode(payload);
}
}
}

View File

@@ -0,0 +1,40 @@
/*
* 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 Antlr.Runtime;
namespace YATC.ASTNodes
{
class FieldInstNode: UtilNode
{
public FieldInstNode(IToken payload)
: base(payload)
{
}
public IdNode IdNode { get { return (IdNode) TigerChildren[0]; } }
public ExpressionNode ExpressionNode{ get { return (ExpressionNode) TigerChildren[1]; } }
}
}

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 Antlr.Runtime;
namespace YATC.ASTNodes
{
/// <summary>
/// Declaracion de parametros de funciones y campos en la declaracion de un record.
/// </summary>
class TypeFieldNode : UtilNode
{
public TypeFieldNode(IToken payload)
: base(payload)
{
}
public TypeNode IdNode { get { return (TypeNode)TigerChildren[0]; } }
public TypeNode TypeNode { get { return (TypeNode)TigerChildren[1]; } }
public string Name { get { return this.IdNode.Name; } }
}
}

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.
*
*/
using Antlr.Runtime;
using YATC.Scope;
namespace YATC.ASTNodes
{
class UnknownNode : TigerNode
{
public UnknownNode(IToken payload)
: base(payload)
{
}
}
}

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.
*
*/
using Antlr.Runtime;
using YATC.Scope;
namespace YATC.ASTNodes
{
abstract class UtilNode : TigerNode
{
protected UtilNode(IToken payload)
: base(payload)
{
}
}
}

View File

@@ -0,0 +1,46 @@
/*
* 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 Antlr.Runtime;
namespace YATC.Grammar
{
public class ParsingException : Exception
{
public ParsingException(string message, Exception innerException)
: base(message, innerException)
{
}
public RecognitionException RecognitionError
{
get
{
return InnerException as RecognitionException;
}
}
}
}

View File

@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("YATC")]
[assembly: AssemblyDescription("Yet Another Tiger Compiler code base")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("YATC")]
[assembly: AssemblyCopyright("Copyright Damian Valdes Santiago & Juan Carlos Pujol Mainegra © 2013-2014")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("791f0f8e-42cf-4906-8b7b-8f7b011f7758")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

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;
}
}

145
YATC/YATC.csproj Normal file
View File

@@ -0,0 +1,145 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{D62DA087-4724-49A3-91B5-83689F442747}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>YATC</RootNamespace>
<AssemblyName>YATC.Base</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Antlr3.Runtime">
<HintPath>..\Antlr3.Runtime.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\Grammar\tigerLexer.cs">
<Link>Grammar\tigerLexer.cs</Link>
</Compile>
<Compile Include="..\Grammar\tigerParser.cs">
<Link>Grammar\tigerParser.cs</Link>
</Compile>
<Compile Include="ASTNodes\LocalNode\DeclarationNode\FunDeclNode.cs" />
<Compile Include="ASTNodes\LocalNode\DeclarationNode\DeclarationNode.cs" />
<Compile Include="ASTNodes\LocalNode\DeclarationNode\FunDeclSeqNode.cs" />
<Compile Include="ASTNodes\LocalNode\DeclarationNode\Node.cs" />
<Compile Include="ASTNodes\LocalNode\DeclarationNode\TypeDeclSeqNode.cs" />
<Compile Include="ASTNodes\LocalNode\DeclarationNode\AliasDeclNode.cs" />
<Compile Include="ASTNodes\LocalNode\ExpressionNode\ArrayInstNode.cs" />
<Compile Include="ASTNodes\LocalNode\ExpressionNode\AtomicNode\AccessNode\AccessNode.cs" />
<Compile Include="ASTNodes\LocalNode\ExpressionNode\AtomicNode\AccessNode\ArrayAccess.cs" />
<Compile Include="ASTNodes\LocalNode\ExpressionNode\AtomicNode\AccessNode\FieldAccessNode.cs" />
<Compile Include="ASTNodes\LocalNode\ExpressionNode\AtomicNode\AtomicNode.cs" />
<Compile Include="ASTNodes\LocalNode\ExpressionNode\AtomicNode\ExprSeqNode.cs" />
<Compile Include="ASTNodes\LocalNode\ExpressionNode\AtomicNode\FunCallNode.cs" />
<Compile Include="ASTNodes\LocalNode\ExpressionNode\AtomicNode\IntNode.cs" />
<Compile Include="ASTNodes\LocalNode\ExpressionNode\AtomicNode\LetNode.cs" />
<Compile Include="ASTNodes\LocalNode\ExpressionNode\AtomicNode\NilNode.cs" />
<Compile Include="ASTNodes\LocalNode\ExpressionNode\AtomicNode\VarAccessNode.cs" />
<Compile Include="ASTNodes\LocalNode\ExpressionNode\AtomicNode\StringNode.cs" />
<Compile Include="ASTNodes\LocalNode\ExpressionNode\BinaryNode\ArithmeticNode\ArithmeticNode.cs" />
<Compile Include="ASTNodes\LocalNode\ExpressionNode\BinaryNode\ArithmeticNode\DivNode.cs" />
<Compile Include="ASTNodes\LocalNode\ExpressionNode\BinaryNode\ArithmeticNode\MinusNode.cs" />
<Compile Include="ASTNodes\LocalNode\ExpressionNode\BinaryNode\ArithmeticNode\MultNode.cs" />
<Compile Include="ASTNodes\LocalNode\ExpressionNode\BinaryNode\ArithmeticNode\PlusNode.cs" />
<Compile Include="ASTNodes\LocalNode\ExpressionNode\BinaryNode\BinaryNode.cs" />
<Compile Include="ASTNodes\LocalNode\ExpressionNode\BinaryNode\LogicNode\AndNode.cs" />
<Compile Include="ASTNodes\LocalNode\ExpressionNode\BinaryNode\LogicNode\LogicNode.cs" />
<Compile Include="ASTNodes\LocalNode\ExpressionNode\BinaryNode\LogicNode\OrNode.cs" />
<Compile Include="ASTNodes\LocalNode\ExpressionNode\BinaryNode\RelationalNode\IdentityNode\EqNode.cs" />
<Compile Include="ASTNodes\LocalNode\ExpressionNode\BinaryNode\RelationalNode\IdentityNode\IdentityNode.cs" />
<Compile Include="ASTNodes\LocalNode\ExpressionNode\BinaryNode\RelationalNode\IdentityNode\NotEqNode.cs" />
<Compile Include="ASTNodes\LocalNode\ExpressionNode\BinaryNode\RelationalNode\OrderNode\GtEqNode.cs" />
<Compile Include="ASTNodes\LocalNode\ExpressionNode\BinaryNode\RelationalNode\OrderNode\GtNode.cs" />
<Compile Include="ASTNodes\LocalNode\ExpressionNode\BinaryNode\RelationalNode\OrderNode\LtEqNode.cs" />
<Compile Include="ASTNodes\LocalNode\ExpressionNode\BinaryNode\RelationalNode\OrderNode\LtNode.cs" />
<Compile Include="ASTNodes\LocalNode\ExpressionNode\BinaryNode\RelationalNode\OrderNode\OrderNode.cs" />
<Compile Include="ASTNodes\LocalNode\ExpressionNode\BinaryNode\RelationalNode\RelationalNode.cs" />
<Compile Include="ASTNodes\LocalNode\ExpressionNode\ExpressionNode.cs" />
<Compile Include="ASTNodes\LocalNode\ExpressionNode\RecordInstNode.cs" />
<Compile Include="ASTNodes\LocalNode\ExpressionNode\NegNode.cs" />
<Compile Include="ASTNodes\LocalNode\LocalNode.cs" />
<Compile Include="ASTNodes\LocalNode\ExpressionNode\StatementNode\AssignNode.cs" />
<Compile Include="ASTNodes\LocalNode\ExpressionNode\StatementNode\BreakNode.cs" />
<Compile Include="ASTNodes\LocalNode\ExpressionNode\StatementNode\EmptyNode.cs" />
<Compile Include="ASTNodes\LocalNode\ExpressionNode\StatementNode\ForNode.cs" />
<Compile Include="ASTNodes\LocalNode\ExpressionNode\StatementNode\IBreakebleNode.cs" />
<Compile Include="ASTNodes\LocalNode\ExpressionNode\AtomicNode\IfNode.cs" />
<Compile Include="ASTNodes\LocalNode\ExpressionNode\StatementNode\StatementNode.cs" />
<Compile Include="ASTNodes\LocalNode\DeclarationNode\TypeDeclNode.cs" />
<Compile Include="ASTNodes\LocalNode\DeclarationNode\VarDeclNode.cs" />
<Compile Include="ASTNodes\LocalNode\ExpressionNode\StatementNode\WhileNode.cs" />
<Compile Include="ASTNodes\ProgramNode.cs" />
<Compile Include="Scope\Report\Item.cs" />
<Compile Include="Scope\Report\Report.cs" />
<Compile Include="Scope\TigerInfo\FunVarInfo.cs" />
<Compile Include="Scope\TigerType\TigerTypeHolder.cs" />
<Compile Include="Scope\TigerType\ArrayType.cs" />
<Compile Include="Scope\TigerType\RecordType.cs" />
<Compile Include="Scope\TigerType\TigerType.cs" />
<Compile Include="ASTNodes\UtilNode\FieldInstNode.cs" />
<Compile Include="ASTNodes\LocalNode\DeclarationNode\ArrayDeclNode.cs" />
<Compile Include="ASTNodes\LocalNode\ExpressionNode\TypeNode\FillInTypeNode.cs" />
<Compile Include="ASTNodes\LocalNode\ExpressionNode\TypeNode\IdNode.cs" />
<Compile Include="ASTNodes\UtilNode\TypeFieldNode.cs" />
<Compile Include="ASTNodes\LocalNode\DeclarationNode\RecordDeclNode.cs" />
<Compile Include="ASTNodes\LocalNode\ExpressionNode\TypeNode\TypeNode.cs" />
<Compile Include="ASTNodes\LocalNode\ExpressionNode\TypeNode\VoidNode.cs" />
<Compile Include="ASTNodes\UtilNode\UnknownNode.cs" />
<Compile Include="ASTNodes\UtilNode\UtilNode.cs" />
<Compile Include="Grammar\ParsingException.cs" />
<Compile Include="Scope\TigerInfo\TigerInfo.cs" />
<Compile Include="ASTNodes\TigerNode.cs" />
<Compile Include="Scope\TigerInfo\TigerTypeInfo.cs" />
<Compile Include="Scope\TigerScope.cs" />
<Compile Include="Scope\TigerInfo\FunctionInfo.cs" />
<Compile Include="Scope\TigerInfo\VariableInfo.cs" />
<Compile Include="ASTNodes\TigerTreeAdaptor.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<Antlr3 Include="..\Grammar\tiger.g">
<Link>Grammar\tiger.g</Link>
<Generator>MSBuild:Compile</Generator>
<CustomToolNamespace>YATC.Grammar</CustomToolNamespace>
</Antlr3>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>