First public commit!
This commit is contained in:
69
YATC/ASTNodes/LocalNode/DeclarationNode/AliasDeclNode.cs
Normal file
69
YATC/ASTNodes/LocalNode/DeclarationNode/AliasDeclNode.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.Reflection.Emit;
|
||||
using YATC.Scope;
|
||||
|
||||
namespace YATC.ASTNodes
|
||||
{
|
||||
class AliasDeclNode : DeclarationNode
|
||||
{
|
||||
public AliasDeclNode(IToken payload)
|
||||
: base(payload)
|
||||
{
|
||||
}
|
||||
|
||||
public TypeNode TypeNode { get { return (TypeNode)TigerChildren[0]; } }
|
||||
|
||||
public override bool CheckHeader(TigerScope scope, Report report, string name)
|
||||
{
|
||||
this.TigerTypeInfo = new TigerTypeInfo(name, new TigerTypeHolder(), false);
|
||||
scope.Add(this.TigerTypeInfo);
|
||||
return true;
|
||||
}
|
||||
|
||||
public override void CheckSemantics(TigerScope scope, Report report)
|
||||
{
|
||||
// type is an alias of another type, then follow.
|
||||
TigerTypeInfo aliasTo = scope.FindTypeInfo(this.TypeNode.Name, false);
|
||||
|
||||
if (aliasTo == null)
|
||||
{
|
||||
report.AddError(this.Line, this.Column, "Alias to undeclared type: '{0}'.", this.TypeNode.Name);
|
||||
this.IsOK = false;
|
||||
return;
|
||||
}
|
||||
|
||||
this.TigerTypeInfo.Holder.TigerType = aliasTo.Holder.TigerType;
|
||||
this.IsOK = true;
|
||||
}
|
||||
|
||||
internal override void GenerateCode(ModuleBuilder moduleBuilder)
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
}
|
||||
}
|
||||
70
YATC/ASTNodes/LocalNode/DeclarationNode/ArrayDeclNode.cs
Normal file
70
YATC/ASTNodes/LocalNode/DeclarationNode/ArrayDeclNode.cs
Normal file
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Yet Another Tiger Compiler (YATC)
|
||||
*
|
||||
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*
|
||||
*/
|
||||
|
||||
using Antlr.Runtime;
|
||||
using System.Reflection.Emit;
|
||||
using YATC.Scope;
|
||||
|
||||
namespace YATC.ASTNodes
|
||||
{
|
||||
internal class ArrayDeclNode: DeclarationNode
|
||||
{
|
||||
public ArrayDeclNode(IToken payload)
|
||||
: base(payload)
|
||||
{
|
||||
}
|
||||
|
||||
private ArrayType _arrayType;
|
||||
public TypeNode ElementTypeNode { get { return (TypeNode)TigerChildren[0]; } }
|
||||
|
||||
public override bool CheckHeader(TigerScope scope, Report report, string name)
|
||||
{
|
||||
this.TigerTypeInfo = new TigerTypeInfo(name, new TigerTypeHolder(), false);
|
||||
scope.Add(this.TigerTypeInfo);
|
||||
return true;
|
||||
}
|
||||
|
||||
public override void CheckSemantics(TigerScope scope, Report report)
|
||||
{
|
||||
TigerTypeInfo elementTigerTypeInfo = scope.FindTypeInfo(this.ElementTypeNode.Name, false);
|
||||
|
||||
if (elementTigerTypeInfo == null)
|
||||
{
|
||||
report.AddError(this.Line, this.Column, "Undeclared type: array element '{0}'.", this.ElementTypeNode.Name);
|
||||
this.IsOK = false;
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
this.TigerTypeInfo.Holder.TigerType = _arrayType = new ArrayType(this.TigerTypeInfo.Name, elementTigerTypeInfo.Holder);
|
||||
this.IsOK = true;
|
||||
}
|
||||
|
||||
internal override void GenerateCode(ModuleBuilder moduleBuilder)
|
||||
{
|
||||
_arrayType.CLRType = _arrayType.ElementTypeHolder.TigerType.GetCLRType().MakeArrayType();
|
||||
}
|
||||
}
|
||||
}
|
||||
50
YATC/ASTNodes/LocalNode/DeclarationNode/DeclarationNode.cs
Normal file
50
YATC/ASTNodes/LocalNode/DeclarationNode/DeclarationNode.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 YATC.Scope;
|
||||
|
||||
namespace YATC.ASTNodes
|
||||
{
|
||||
abstract class DeclarationNode : LocalNode
|
||||
{
|
||||
protected DeclarationNode(IToken payload)
|
||||
: base(payload)
|
||||
{
|
||||
}
|
||||
|
||||
public TigerTypeInfo TigerTypeInfo { get; protected set; }
|
||||
|
||||
public virtual bool CheckHeader(TigerScope scope, Report report, string name)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Por defecto es false
|
||||
/// </summary>
|
||||
public bool IsOK { get; set; }
|
||||
}
|
||||
}
|
||||
154
YATC/ASTNodes/LocalNode/DeclarationNode/FunDeclSeqNode.cs
Normal file
154
YATC/ASTNodes/LocalNode/DeclarationNode/FunDeclSeqNode.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.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection.Emit;
|
||||
using YATC.Scope;
|
||||
|
||||
namespace YATC.ASTNodes
|
||||
{
|
||||
internal class FunDeclSeqNode : DeclarationNode
|
||||
{
|
||||
public FunDeclSeqNode(IToken payload)
|
||||
: base(payload)
|
||||
{
|
||||
}
|
||||
|
||||
public FunDeclNode[] FunDeclNodes { get { return TigerChildren.Cast<FunDeclNode>().ToArray(); } }
|
||||
|
||||
public ParameterExpression[] FunctionClousures { get; private set; }
|
||||
public Expression[] FunctionAssigns { get; private set; }
|
||||
|
||||
public override void CheckSemantics(TigerScope scope, Report report)
|
||||
{
|
||||
for (int index = 0; index < FunDeclNodes.Length; index++)
|
||||
{
|
||||
var funDeclNode = FunDeclNodes[index];
|
||||
|
||||
if (scope.CanFindFunVarInfo(funDeclNode.Name, true))
|
||||
{
|
||||
report.AddError(this.Line, this.Column,
|
||||
"Redeclared local function or variable name: '{0}'.", funDeclNode.Name);
|
||||
this.IsOK = false;
|
||||
return;
|
||||
}
|
||||
|
||||
FunctionInfo outerFunction = scope.FindFunctionInfo(funDeclNode.Name, false);
|
||||
if (outerFunction != null)
|
||||
if (outerFunction.IsStandard)
|
||||
{
|
||||
report.AddError(this.Line, this.Column,
|
||||
"Redeclared standard function or procedure: '{0}'.", funDeclNode.Name);
|
||||
this.IsOK = false;
|
||||
return;
|
||||
}
|
||||
else
|
||||
report.AddWarning(this.Line, this.Column,
|
||||
"Function name hides outer scope variable or function: '{0}'.",
|
||||
funDeclNode.Name);
|
||||
|
||||
// Checking not repiting parameter names
|
||||
var parameterNames = new HashSet<string>();
|
||||
var parameterInfo = new VariableInfo[funDeclNode.Params.Length];
|
||||
for (int i = 0; i < parameterInfo.Length; i++)
|
||||
{
|
||||
var parameter = funDeclNode.Params[i];
|
||||
if (!parameterNames.Add(parameter.Name))
|
||||
{
|
||||
report.AddError(this.Line, this.Column,
|
||||
"Redeclared function or procedure formal parameter name: '{0}'.", parameter.Name);
|
||||
this.IsOK = false;
|
||||
return;
|
||||
}
|
||||
|
||||
parameter.TypeNode.CheckSemantics(scope, report);
|
||||
TigerTypeInfo paramInfo = scope.FindTypeInfo(parameter.TypeNode.Name, false);
|
||||
|
||||
if (paramInfo == null)
|
||||
{
|
||||
report.AddError(this.Line, this.Column,
|
||||
"Undeclared type: Function or procedure parameter: " +
|
||||
"'{0}' of formal parameter '{1}'.", parameter.TypeNode.Name, parameter.Name);
|
||||
this.IsOK = false;
|
||||
return;
|
||||
}
|
||||
|
||||
parameterInfo[i] = new VariableInfo(parameter.Name, paramInfo.Holder, true);
|
||||
}
|
||||
|
||||
// Checking return type
|
||||
TigerType returnType;
|
||||
if (funDeclNode.TypeNode != null)
|
||||
{
|
||||
TigerTypeInfo returnInfo = scope.FindTypeInfo(funDeclNode.TypeNode.Name, false);
|
||||
if (returnInfo == null)
|
||||
{
|
||||
report.AddError(this.Line, this.Column,
|
||||
"Undeclared function or procedure return type: {0}", funDeclNode.TypeNode.Name);
|
||||
this.IsOK = false;
|
||||
return;
|
||||
}
|
||||
returnType = returnInfo.Holder.TigerType;
|
||||
}
|
||||
else
|
||||
returnType = TigerType.Void;
|
||||
|
||||
var functionInfo = new FunctionInfo(funDeclNode.Name, parameterInfo, new TigerTypeHolder(returnType), false);
|
||||
scope.Add(functionInfo);
|
||||
}
|
||||
|
||||
// Checking children
|
||||
bool areAllOk = true;
|
||||
foreach (var funDeclNode in FunDeclNodes)
|
||||
{
|
||||
funDeclNode.CheckSemantics(scope, report);
|
||||
if (!funDeclNode.IsOK)
|
||||
areAllOk = false;
|
||||
}
|
||||
|
||||
this.IsOK = areAllOk;
|
||||
}
|
||||
|
||||
internal override void GenerateCode(ModuleBuilder moduleBuilder)
|
||||
{
|
||||
FunctionClousures = new ParameterExpression[this.FunDeclNodes.Length];
|
||||
FunctionAssigns = new Expression[this.FunDeclNodes.Length];
|
||||
for (int i = 0; i < this.FunDeclNodes.Length; i++)
|
||||
FunctionClousures[i] = this.FunDeclNodes[i].GenerateHeaderCode();
|
||||
|
||||
for (int i = 0; i < this.FunDeclNodes.Length; i++)
|
||||
this.FunDeclNodes[i].GenerateCode(moduleBuilder);
|
||||
|
||||
for (int i = 0; i < this.FunDeclNodes.Length; i++)
|
||||
FunctionAssigns[i] = Expression.Assign(
|
||||
this.FunDeclNodes[i].FunctionInfo.LambdaExpression,
|
||||
this.FunDeclNodes[i].VmExpression
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
144
YATC/ASTNodes/LocalNode/DeclarationNode/FundeclNode.cs
Normal file
144
YATC/ASTNodes/LocalNode/DeclarationNode/FundeclNode.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;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection.Emit;
|
||||
using YATC.Scope;
|
||||
|
||||
namespace YATC.ASTNodes
|
||||
{
|
||||
class FunDeclNode : DeclarationNode
|
||||
{
|
||||
public FunDeclNode(IToken payload)
|
||||
: base(payload)
|
||||
{
|
||||
}
|
||||
|
||||
public TypeNode IdNode { get { return (TypeNode)TigerChildren[0]; } }
|
||||
public TypeFieldNode[] Params
|
||||
{
|
||||
get { return TigerChildren[1].TigerChildren.Cast<TypeFieldNode>().ToArray(); }
|
||||
}
|
||||
public TypeNode TypeNode
|
||||
{
|
||||
get { return TigerChildren[2].TigerChildren.Length > 0 ?
|
||||
(TypeNode)TigerChildren[2].TigerChildren[0] :
|
||||
null; }
|
||||
}
|
||||
public ExpressionNode ExpressionBodyNode { get { return (ExpressionNode)TigerChildren[3]; } }
|
||||
|
||||
public string Name { get { return this.IdNode.Name; } }
|
||||
public FunctionInfo FunctionInfo;
|
||||
|
||||
public override void CheckSemantics(TigerScope scope, Report report)
|
||||
{
|
||||
FunctionInfo = scope.FindFunctionInfo(this.Name, true);
|
||||
|
||||
if (FunctionInfo == null)
|
||||
throw new NullReferenceException();
|
||||
|
||||
var innerScope = scope.CreateChildScope();
|
||||
foreach (var parameterInfo in FunctionInfo.ParameterInfo)
|
||||
{
|
||||
if (innerScope.CanFindFunVarInfo(parameterInfo.Name, false))
|
||||
report.AddWarning(this.Line, this.Column,
|
||||
"Parameter name hides outer scope variable or function in '{0}': '{1}'.",
|
||||
this.Name, parameterInfo.Name);
|
||||
innerScope.Add(parameterInfo);
|
||||
}
|
||||
|
||||
this.ExpressionBodyNode.CheckSemantics(innerScope, report);
|
||||
|
||||
// chequeando que el tipo de retorno sea el mismo que el tipo de la expresion,
|
||||
// no se hace salvedad en el caso de los procedures.
|
||||
if (!this.ExpressionBodyNode.TigerType.IsAssignableTo(FunctionInfo.Holder.TigerType))
|
||||
{
|
||||
report.AddError(this.Line, this.Column,
|
||||
"Type mismatch: Function or procedure return and expression types in '{0}': " +
|
||||
"Expecting '{1}' and '{2}' found.",
|
||||
this.Name, FunctionInfo.Holder.TigerType.Name, this.ExpressionBodyNode.TigerType.Name);
|
||||
this.IsOK = false;
|
||||
return;
|
||||
}
|
||||
|
||||
this.IsOK = true;
|
||||
}
|
||||
|
||||
public Type DelegateType;
|
||||
|
||||
internal ParameterExpression GenerateHeaderCode()
|
||||
{
|
||||
bool funRets = FunctionInfo.Holder.TigerType.Basetype != BaseType.Void;
|
||||
|
||||
Type[] paramTypes = new Type[this.FunctionInfo.ParameterInfo.Length + (funRets ? 1 : 0)];
|
||||
for (int i = 0; i < this.FunctionInfo.ParameterInfo.Length; i++)
|
||||
paramTypes[i] = this.FunctionInfo.ParameterInfo[i].Holder.TigerType.GetCLRType();
|
||||
|
||||
if (funRets)
|
||||
paramTypes[this.Params.Length] = this.FunctionInfo.Holder.TigerType.GetCLRType();
|
||||
|
||||
DelegateType = funRets
|
||||
? Expression.GetFuncType(paramTypes)
|
||||
: Expression.GetActionType(paramTypes);
|
||||
|
||||
var paramExpr = Expression.Parameter(DelegateType);
|
||||
|
||||
FunctionInfo.LambdaExpression = paramExpr;
|
||||
return paramExpr;
|
||||
}
|
||||
|
||||
internal override void GenerateCode(ModuleBuilder moduleBuilder)
|
||||
{
|
||||
var paramsExprs = new ParameterExpression[this.Params.Length];
|
||||
for (int i = 0; i < Params.Length; i++)
|
||||
{
|
||||
paramsExprs[i] = Expression.Parameter(this.FunctionInfo.ParameterInfo[i].Holder.TigerType.GetCLRType());
|
||||
FunctionInfo.ParameterInfo[i].ParameterExpression = paramsExprs[i];
|
||||
}
|
||||
|
||||
this.ExpressionBodyNode.GenerateCode(moduleBuilder);
|
||||
|
||||
Expression bodyExpr = this.FunctionInfo.Holder.TigerType.Basetype == BaseType.Void
|
||||
? Expression.Block(
|
||||
this.ExpressionBodyNode.VmExpression,
|
||||
Expression.Empty()
|
||||
)
|
||||
: (Expression)Expression.Convert(
|
||||
ExpressionBodyNode.VmExpression,
|
||||
this.FunctionInfo.Holder.TigerType.GetCLRType()
|
||||
);
|
||||
|
||||
this.VmExpression = Expression.Lambda(
|
||||
//DelegateType,
|
||||
bodyExpr,
|
||||
this.Name,
|
||||
paramsExprs
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
117
YATC/ASTNodes/LocalNode/DeclarationNode/Node.cs
Normal file
117
YATC/ASTNodes/LocalNode/DeclarationNode/Node.cs
Normal file
@@ -0,0 +1,117 @@
|
||||
/*
|
||||
* Yet Another Tiger Compiler (YATC)
|
||||
*
|
||||
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*
|
||||
*/
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace YATC.ASTNodes
|
||||
{
|
||||
internal class Node<T>
|
||||
{
|
||||
public enum ColorCode
|
||||
{
|
||||
White = 0,
|
||||
Unvisited = 0,
|
||||
Black = 1,
|
||||
Done = 1,
|
||||
Gray = 2,
|
||||
StillNotDone = 2
|
||||
}
|
||||
|
||||
public ColorCode Color { get; private set; }
|
||||
public Node<T> Parent { get; private set; }
|
||||
public readonly T Value;
|
||||
|
||||
public Node(T value)
|
||||
{
|
||||
this.Value = value;
|
||||
}
|
||||
|
||||
private readonly LinkedList<Node<T>> _pred = new LinkedList<Node<T>>();
|
||||
private readonly LinkedList<Node<T>> _succ = new LinkedList<Node<T>>();
|
||||
|
||||
public IEnumerable<Node<T>> Pred { get { return _pred; } }
|
||||
public IEnumerable<Node<T>> Succ { get { return _succ; } }
|
||||
public IEnumerable<Node<T>> Adj { get { return _pred.Concat(_succ); } }
|
||||
|
||||
public void AddSucc(Node<T> node) { _succ.AddLast(node); }
|
||||
public void AddPred(Node<T> node) { _pred.AddLast(node); }
|
||||
|
||||
private static Node<T> DFS(IEnumerable<Node<T>> nodes, bool checkCycles, out LinkedList<Node<T>> linkedList)
|
||||
{
|
||||
int time = 0;
|
||||
linkedList = new LinkedList<Node<T>>();
|
||||
foreach (var node in nodes.Where(node => node.Color == ColorCode.White))
|
||||
{
|
||||
var cycleEnd = node.Visit(ref time, checkCycles, linkedList);
|
||||
if (cycleEnd != null)
|
||||
return cycleEnd;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private Node<T> Visit(ref int time, bool checkCycles, LinkedList<Node<T>> topologicalSort)
|
||||
{
|
||||
this.Color = ColorCode.Gray;
|
||||
foreach (var node in this.Succ)
|
||||
{
|
||||
// by the Proof of Theorem 22.12, page 614, (u,v) is a back edge iff v is Gray
|
||||
// from Cormen et al. - Introduction To Algorithms, 3rd edition
|
||||
if (node.Color == ColorCode.Gray && checkCycles)
|
||||
return this;
|
||||
|
||||
if (node.Color == ColorCode.White)
|
||||
{
|
||||
node.Parent = this;
|
||||
var cycleEnd = node.Visit(ref time, checkCycles, topologicalSort);
|
||||
if (cycleEnd != null)
|
||||
return cycleEnd;
|
||||
}
|
||||
}
|
||||
this.Color = ColorCode.Black;
|
||||
topologicalSort.AddFirst(this);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Node<T> TopologicalSort(IEnumerable<Node<T>> nodes, out LinkedList<Node<T>> linkedList)
|
||||
{
|
||||
return DFS(nodes, true, out linkedList);
|
||||
}
|
||||
|
||||
public class NodeValueEqualityComparer : IEqualityComparer<Node<T>>
|
||||
{
|
||||
public bool Equals(Node<T> x, Node<T> y)
|
||||
{
|
||||
return x.Value.Equals(y.Value);
|
||||
}
|
||||
|
||||
public int GetHashCode(Node<T> obj)
|
||||
{
|
||||
return obj.Value.GetHashCode();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
125
YATC/ASTNodes/LocalNode/DeclarationNode/RecordDeclNode.cs
Normal file
125
YATC/ASTNodes/LocalNode/DeclarationNode/RecordDeclNode.cs
Normal file
@@ -0,0 +1,125 @@
|
||||
/*
|
||||
* Yet Another Tiger Compiler (YATC)
|
||||
*
|
||||
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*
|
||||
*/
|
||||
|
||||
using Antlr.Runtime;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Reflection.Emit;
|
||||
using YATC.Scope;
|
||||
|
||||
namespace YATC.ASTNodes
|
||||
{
|
||||
internal class RecordDeclNode : DeclarationNode
|
||||
{
|
||||
public RecordDeclNode(IToken payload)
|
||||
: base(payload)
|
||||
{
|
||||
}
|
||||
|
||||
public TypeFieldNode[] TypeFieldNodes
|
||||
{
|
||||
get
|
||||
{
|
||||
return TigerChildren.Length > 0 ? TigerChildren.Cast<TypeFieldNode>().ToArray() : new TypeFieldNode[] {};
|
||||
}
|
||||
}
|
||||
|
||||
public TypeBuilder TypeBuilder { get; private set; }
|
||||
|
||||
public override bool CheckHeader(TigerScope scope, Report report, string name)
|
||||
{
|
||||
this.TigerTypeInfo = new TigerTypeInfo(name, new TigerTypeHolder(), false);
|
||||
scope.Add(this.TigerTypeInfo);
|
||||
return true;
|
||||
}
|
||||
|
||||
public override void CheckSemantics(TigerScope scope, Report report)
|
||||
{
|
||||
var hash = new HashSet<string>();
|
||||
foreach (var typeFieldNode in this.TypeFieldNodes)
|
||||
{
|
||||
typeFieldNode.TypeNode.CheckSemantics(scope, report);
|
||||
|
||||
if (!typeFieldNode.TypeNode.IsOk)
|
||||
{
|
||||
this.TigerTypeInfo.Holder.TigerType = TigerType.Error;
|
||||
this.IsOK = false;
|
||||
return;
|
||||
}
|
||||
|
||||
string name = typeFieldNode.IdNode.Name;
|
||||
if (!hash.Add(name))
|
||||
{
|
||||
report.AddError(this.Line, this.Column, "Redeclared field name: '{0}'.", name);
|
||||
this.TigerTypeInfo.Holder.TigerType = TigerType.Error;
|
||||
this.IsOK = false;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
VariableInfo[] fieldInfos = new VariableInfo[this.TypeFieldNodes.Length];
|
||||
|
||||
for (int i = 0; i < fieldInfos.Length; i++)
|
||||
{
|
||||
TigerTypeInfo fieldTigerTypeInfo = scope.FindTypeInfo(this.TypeFieldNodes[i].TypeNode.Name, false);
|
||||
if (fieldTigerTypeInfo == null)
|
||||
{
|
||||
report.AddError(this.Line, this.Column, "Undeclared field type: '{0}'.",
|
||||
this.TypeFieldNodes[i].TypeNode.Name);
|
||||
this.TigerTypeInfo.Holder.TigerType = TigerType.Error;
|
||||
this.IsOK = false;
|
||||
return;
|
||||
}
|
||||
|
||||
fieldInfos[i] = new VariableInfo(this.TypeFieldNodes[i].Name, fieldTigerTypeInfo.Holder, false);
|
||||
}
|
||||
|
||||
this.TigerTypeInfo.Holder.TigerType = new RecordType(this.TigerTypeInfo.Name, fieldInfos);
|
||||
this.IsOK = true;
|
||||
}
|
||||
|
||||
public void GenerateHeaderCode(ModuleBuilder moduleBuilder)
|
||||
{
|
||||
string name = string.Format("{0}_{1}", this.TigerTypeInfo.Name, ProgramNode.RecordNumber++);
|
||||
this.TigerTypeInfo.Holder.TigerType.CLRType =
|
||||
this.TypeBuilder = moduleBuilder.DefineType(name, TypeAttributes.Public);
|
||||
}
|
||||
|
||||
internal override void GenerateCode(ModuleBuilder moduleBuilder)
|
||||
{
|
||||
foreach (var variableInfo in ((RecordType)this.TigerTypeInfo.Holder.TigerType).FieldInfos)
|
||||
{
|
||||
this.TypeBuilder.DefineField(
|
||||
variableInfo.Name,
|
||||
variableInfo.Holder.TigerType.GetCLRType(),
|
||||
FieldAttributes.Public);
|
||||
}
|
||||
|
||||
// Finish the type.
|
||||
this.TigerTypeInfo.Holder.TigerType.CLRType = this.TypeBuilder.CreateType();
|
||||
}
|
||||
}
|
||||
}
|
||||
190
YATC/ASTNodes/LocalNode/DeclarationNode/TypeDeclSeqNode.cs
Normal file
190
YATC/ASTNodes/LocalNode/DeclarationNode/TypeDeclSeqNode.cs
Normal file
@@ -0,0 +1,190 @@
|
||||
/*
|
||||
* Yet Another Tiger Compiler (YATC)
|
||||
*
|
||||
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*
|
||||
*/
|
||||
|
||||
using Antlr.Runtime;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Reflection.Emit;
|
||||
using System.Text;
|
||||
using YATC.Scope;
|
||||
|
||||
namespace YATC.ASTNodes
|
||||
{
|
||||
internal class TypeDeclSeqNode : DeclarationNode
|
||||
{
|
||||
private LinkedList<Node<TypeDeclNode>> _topologicalSort;
|
||||
|
||||
public TypeDeclSeqNode(IToken payload)
|
||||
: base(payload)
|
||||
{
|
||||
}
|
||||
|
||||
public IEnumerable<TypeDeclNode> TypeDeclNodes { get { return TigerChildren.Cast<TypeDeclNode>(); } }
|
||||
|
||||
/// <summary>
|
||||
/// Por pasos:
|
||||
/// 1ro : Agregar cabezas de tipos
|
||||
/// 2do : Construir grafo y revisar orden topologico
|
||||
/// 3ro : En el orden dado hacer chequeo semantico
|
||||
/// </summary>
|
||||
/// <param name="scope"></param>
|
||||
/// <param name="report"></param>
|
||||
public override void CheckSemantics(TigerScope scope, Report report)
|
||||
{
|
||||
var checkedNodes = new Dictionary<string, TypeDeclNode>();
|
||||
|
||||
// 1. agregar cabezas de tipos
|
||||
foreach (var typeDeclNode in TypeDeclNodes)
|
||||
{
|
||||
if (!checkedNodes.ContainsKey(typeDeclNode.Name))
|
||||
checkedNodes.Add(typeDeclNode.Name, typeDeclNode);
|
||||
else
|
||||
{
|
||||
report.AddError(this.Line, this.Column,
|
||||
"Redeclared name in type declaration sequence: '{0}'.", typeDeclNode.Name);
|
||||
this.IsOK = false;
|
||||
return;
|
||||
}
|
||||
|
||||
// chequeamos el header, mayormente por problemas de redeclaracion local
|
||||
// o global pero de tipos standard
|
||||
if (!typeDeclNode.CheckHeader(scope, report, string.Empty))
|
||||
{
|
||||
this.IsOK = false;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// 2. DAG
|
||||
|
||||
// 2.1 construir grafo
|
||||
|
||||
var graph = new Dictionary<TypeDeclNode, Node<TypeDeclNode>>();
|
||||
|
||||
foreach (var typeDeclNode in TypeDeclNodes)
|
||||
{
|
||||
string edgeNameTo = string.Empty;
|
||||
if (typeDeclNode.IsAliasNode)
|
||||
edgeNameTo = ((AliasDeclNode)typeDeclNode.DeclarationNode).TypeNode.Name;
|
||||
else if (typeDeclNode.IsArrayNode)
|
||||
edgeNameTo = ((ArrayDeclNode)typeDeclNode.DeclarationNode).ElementTypeNode.Name;
|
||||
|
||||
Node<TypeDeclNode> thisNode;
|
||||
if (!graph.TryGetValue(typeDeclNode, out thisNode))
|
||||
{
|
||||
thisNode = new Node<TypeDeclNode>(typeDeclNode);
|
||||
graph.Add(typeDeclNode, thisNode);
|
||||
}
|
||||
|
||||
if (edgeNameTo == string.Empty)
|
||||
continue;
|
||||
|
||||
TypeDeclNode edgeTo;
|
||||
if (checkedNodes.TryGetValue(edgeNameTo, out edgeTo))
|
||||
{
|
||||
Node<TypeDeclNode> node;
|
||||
if (!graph.TryGetValue(edgeTo, out node))
|
||||
{
|
||||
node = new Node<TypeDeclNode>(edgeTo);
|
||||
graph.Add(edgeTo, node);
|
||||
}
|
||||
|
||||
node.AddSucc(thisNode);
|
||||
}
|
||||
}
|
||||
|
||||
// 2.2 obtener orden topologico (detectando ciclos)
|
||||
|
||||
#if DEBUG
|
||||
foreach (var edge in graph.Values)
|
||||
{
|
||||
var from = edge.Succ.Any() ? edge.Succ.FirstOrDefault().Value.IdNode.ToString() : "<end>";
|
||||
var to = edge.Value.IdNode;
|
||||
Debug.WriteLine("{0} -> {1}", from, to);
|
||||
}
|
||||
#endif
|
||||
|
||||
var cycleEnd = Node<TypeDeclNode>.TopologicalSort(graph.Values, out _topologicalSort);
|
||||
|
||||
#if DEBUG
|
||||
foreach (var node in _topologicalSort)
|
||||
Debug.WriteLine("Adding {0}", node.Value.IdNode);
|
||||
#endif
|
||||
|
||||
if (cycleEnd != null)
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
for (var current = cycleEnd; current != null; current = current.Parent)
|
||||
sb.Append(current.Value.IdNode.Name + " -> ");
|
||||
|
||||
sb.Append(cycleEnd.Value.IdNode.Name);
|
||||
report.AddError(this.Line, this.Column,
|
||||
"Undetected record in recursive type declaration sequence. Cycle definition is: {0}",
|
||||
sb.ToString());
|
||||
this.IsOK = false;
|
||||
return;
|
||||
}
|
||||
|
||||
// 3. chequear semantica de los nodos en el orden topologico
|
||||
foreach (var node in _topologicalSort)
|
||||
{
|
||||
Debug.WriteLine("Checking {0}", node.Value.IdNode);
|
||||
node.Value.CheckSemantics(scope, report);
|
||||
if (!node.Value.IsOK)
|
||||
{
|
||||
this.IsOK = false;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
//foreach (var node in linkedList)
|
||||
//{
|
||||
// node.
|
||||
//}
|
||||
|
||||
this.IsOK = true;
|
||||
}
|
||||
|
||||
internal override void GenerateCode(ModuleBuilder moduleBuilder)
|
||||
{
|
||||
// records go first
|
||||
|
||||
IEnumerable<RecordDeclNode> recordDeclNodes =
|
||||
_topologicalSort.Select(x => x.Value).
|
||||
Where(x => x.DeclarationNode is RecordDeclNode).
|
||||
Select(x => x.DeclarationNode).
|
||||
Cast<RecordDeclNode>();
|
||||
|
||||
foreach (var recordDeclNode in recordDeclNodes)
|
||||
recordDeclNode.GenerateHeaderCode(moduleBuilder);
|
||||
|
||||
// everything
|
||||
|
||||
foreach (var declarationNode in _topologicalSort.Select(x => x.Value))
|
||||
declarationNode.GenerateCode(moduleBuilder);
|
||||
}
|
||||
}
|
||||
}
|
||||
84
YATC/ASTNodes/LocalNode/DeclarationNode/TypedeclNode.cs
Normal file
84
YATC/ASTNodes/LocalNode/DeclarationNode/TypedeclNode.cs
Normal file
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* Yet Another Tiger Compiler (YATC)
|
||||
*
|
||||
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*
|
||||
*/
|
||||
|
||||
using Antlr.Runtime;
|
||||
using System.Reflection.Emit;
|
||||
using YATC.Scope;
|
||||
|
||||
namespace YATC.ASTNodes
|
||||
{
|
||||
class TypeDeclNode : DeclarationNode
|
||||
{
|
||||
public TypeDeclNode(IToken payload)
|
||||
: base(payload)
|
||||
{
|
||||
}
|
||||
|
||||
public string Name { get { return this.IdNode.Name; } }
|
||||
|
||||
public IdNode IdNode { get { return (IdNode)TigerChildren[0]; } }
|
||||
public DeclarationNode DeclarationNode { get { return (DeclarationNode)TigerChildren[1]; } }
|
||||
|
||||
public bool IsAliasNode { get { return this.DeclarationNode is AliasDeclNode; } }
|
||||
public bool IsRecordNode { get { return this.DeclarationNode is RecordDeclNode; } }
|
||||
public bool IsArrayNode { get { return this.DeclarationNode is ArrayDeclNode; } }
|
||||
|
||||
public override bool CheckHeader(TigerScope scope, Report report, string name)
|
||||
{
|
||||
if (scope.CanFindTypeInfo(this.Name, true))
|
||||
{
|
||||
report.AddError(this.Line, this.Column, "Redeclared local type: '{0}'.", this.Name);
|
||||
this.IsOK = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
TigerTypeInfo outerTypeInfo = scope.FindTypeInfo(this.Name, false);
|
||||
if (outerTypeInfo != null)
|
||||
if (outerTypeInfo.IsStandard)
|
||||
{
|
||||
report.AddError(this.Line, this.Column,
|
||||
"Redeclared standard type: '{0}'.",
|
||||
this.Name);
|
||||
this.IsOK = false;
|
||||
return false;
|
||||
}
|
||||
else
|
||||
report.AddWarning(this.Line, this.Column, "Type name hides outer scope type: '{0}'.", this.Name);
|
||||
|
||||
return this.DeclarationNode.CheckHeader(scope, report, this.IdNode.Name);
|
||||
}
|
||||
|
||||
public override void CheckSemantics(TigerScope scope, Report report)
|
||||
{
|
||||
this.DeclarationNode.CheckSemantics(scope, report);
|
||||
this.IsOK = this.DeclarationNode.IsOK;
|
||||
}
|
||||
|
||||
internal override void GenerateCode(ModuleBuilder moduleBuilder)
|
||||
{
|
||||
this.DeclarationNode.GenerateCode(moduleBuilder);
|
||||
}
|
||||
}
|
||||
}
|
||||
140
YATC/ASTNodes/LocalNode/DeclarationNode/VardeclNode.cs
Normal file
140
YATC/ASTNodes/LocalNode/DeclarationNode/VardeclNode.cs
Normal file
@@ -0,0 +1,140 @@
|
||||
/*
|
||||
* Yet Another Tiger Compiler (YATC)
|
||||
*
|
||||
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*
|
||||
*/
|
||||
|
||||
using Antlr.Runtime;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection.Emit;
|
||||
using YATC.Scope;
|
||||
|
||||
namespace YATC.ASTNodes
|
||||
{
|
||||
class VarDeclNode : DeclarationNode
|
||||
{
|
||||
public VarDeclNode(IToken payload)
|
||||
: base(payload)
|
||||
{
|
||||
}
|
||||
|
||||
public TypeNode IdNode { get { return (TypeNode)TigerChildren[0]; } }
|
||||
public TypeNode TypeNode { get { return (TypeNode)TigerChildren[1]; } }
|
||||
public ExpressionNode RightExpressionNode { get { return (ExpressionNode)TigerChildren[2]; } }
|
||||
|
||||
public VariableInfo VariableInfo { get; set; }
|
||||
public bool IsAutoVariable { get { return this.TypeNode is FillInTypeNode; } }
|
||||
|
||||
public override void CheckSemantics(TigerScope scope, Report report)
|
||||
{
|
||||
string name = this.IdNode.Name;
|
||||
|
||||
if (scope.CanFindFunVarInfo(name, true))
|
||||
{
|
||||
report.AddError(this.Line, this.Column,
|
||||
"Redeclared local variable or function: '{0}'.",
|
||||
name);
|
||||
this.IsOK = false;
|
||||
return;
|
||||
}
|
||||
|
||||
FunctionInfo outerfunctionInfo = scope.FindFunctionInfo(name, false);
|
||||
if (outerfunctionInfo != null)
|
||||
{
|
||||
if (outerfunctionInfo.IsStandard)
|
||||
{
|
||||
report.AddError(this.Line, this.Column,
|
||||
"Cannot define variable name with standard function: '{0}'.",
|
||||
name);
|
||||
this.IsOK = false;
|
||||
return;
|
||||
}
|
||||
else
|
||||
report.AddWarning(this.Line, this.Column,
|
||||
"Variable name hides outer scope variable or function: '{0}'.",
|
||||
name);
|
||||
}
|
||||
this.RightExpressionNode.CheckSemantics(scope, report);
|
||||
|
||||
if (!this.RightExpressionNode.IsOk)
|
||||
{
|
||||
this.IsOK = false;
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.RightExpressionNode.TigerType.Equals(TigerType.Void))
|
||||
{
|
||||
report.AddError(this.Line, this.Column, "Right hand side expression must evaluate to a returning value.");
|
||||
this.IsOK = false;
|
||||
return;
|
||||
}
|
||||
|
||||
this.TypeNode.CheckSemantics(scope, report);
|
||||
TigerType returnType = this.RightExpressionNode.TigerType;
|
||||
|
||||
if (!this.IsAutoVariable)
|
||||
{
|
||||
TigerTypeInfo tigerTypeInfo = scope.FindTypeInfo(this.TypeNode.Name, false);
|
||||
if (tigerTypeInfo == null)
|
||||
{
|
||||
report.AddError(this.Line, this.Column, "Undeclared type: '{0}'.", this.TypeNode.Name);
|
||||
this.IsOK = false;
|
||||
return;
|
||||
}
|
||||
|
||||
/* Assignment */
|
||||
if (!this.RightExpressionNode.TigerType.IsAssignableTo(tigerTypeInfo.Holder.TigerType))
|
||||
{
|
||||
report.AddError(this.Line, this.Column,
|
||||
"Type mismatch: Variable declaration and expression do not match " +
|
||||
"for '{0}': expecting '{1}' and '{2}' found.",
|
||||
name, tigerTypeInfo.Holder.TigerType.Name, this.RightExpressionNode.TigerType.Name);
|
||||
this.IsOK = false;
|
||||
return;
|
||||
}
|
||||
|
||||
returnType = tigerTypeInfo.Holder.TigerType;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (returnType.Basetype == BaseType.Nil)
|
||||
{
|
||||
report.AddError(this.Line, this.Column,
|
||||
"An automatic variable cannot be declared from nil expression.");
|
||||
this.IsOK = false;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
this.VariableInfo = new VariableInfo(name, new TigerTypeHolder(returnType), false);
|
||||
scope.Add(VariableInfo);
|
||||
this.IsOK = true;
|
||||
}
|
||||
|
||||
internal override void GenerateCode(ModuleBuilder moduleBuilder)
|
||||
{
|
||||
this.RightExpressionNode.GenerateCode(moduleBuilder);
|
||||
this.VariableInfo.ParameterExpression =
|
||||
Expression.Parameter(this.VariableInfo.Holder.TigerType.GetCLRType());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user