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