First public commit!
This commit is contained in:
144
YATC/ASTNodes/LocalNode/ExpressionNode/ArrayInstNode.cs
Normal file
144
YATC/ASTNodes/LocalNode/ExpressionNode/ArrayInstNode.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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; }
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
137
YATC/ASTNodes/LocalNode/ExpressionNode/AtomicNode/FuncallNode.cs
Normal file
137
YATC/ASTNodes/LocalNode/ExpressionNode/AtomicNode/FuncallNode.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
138
YATC/ASTNodes/LocalNode/ExpressionNode/AtomicNode/IfNode.cs
Normal file
138
YATC/ASTNodes/LocalNode/ExpressionNode/AtomicNode/IfNode.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
58
YATC/ASTNodes/LocalNode/ExpressionNode/AtomicNode/IntNode.cs
Normal file
58
YATC/ASTNodes/LocalNode/ExpressionNode/AtomicNode/IntNode.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
131
YATC/ASTNodes/LocalNode/ExpressionNode/AtomicNode/LetNode.cs
Normal file
131
YATC/ASTNodes/LocalNode/ExpressionNode/AtomicNode/LetNode.cs
Normal 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));
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
50
YATC/ASTNodes/LocalNode/ExpressionNode/AtomicNode/NilNode.cs
Normal file
50
YATC/ASTNodes/LocalNode/ExpressionNode/AtomicNode/NilNode.cs
Normal 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)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
54
YATC/ASTNodes/LocalNode/ExpressionNode/ExpressionNode.cs
Normal file
54
YATC/ASTNodes/LocalNode/ExpressionNode/ExpressionNode.cs
Normal 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); } }
|
||||
}
|
||||
}
|
||||
77
YATC/ASTNodes/LocalNode/ExpressionNode/NegNode.cs
Normal file
77
YATC/ASTNodes/LocalNode/ExpressionNode/NegNode.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
154
YATC/ASTNodes/LocalNode/ExpressionNode/RecordInstNode.cs
Normal file
154
YATC/ASTNodes/LocalNode/ExpressionNode/RecordInstNode.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
127
YATC/ASTNodes/LocalNode/ExpressionNode/StatementNode/ForNode.cs
Normal file
127
YATC/ASTNodes/LocalNode/ExpressionNode/StatementNode/ForNode.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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; }
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
44
YATC/ASTNodes/LocalNode/ExpressionNode/TypeNode/IdNode.cs
Normal file
44
YATC/ASTNodes/LocalNode/ExpressionNode/TypeNode/IdNode.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
69
YATC/ASTNodes/LocalNode/ExpressionNode/TypeNode/TypeNode.cs
Normal file
69
YATC/ASTNodes/LocalNode/ExpressionNode/TypeNode/TypeNode.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
42
YATC/ASTNodes/LocalNode/ExpressionNode/TypeNode/VoidNode.cs
Normal file
42
YATC/ASTNodes/LocalNode/ExpressionNode/TypeNode/VoidNode.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user