bool AcceptType(IdentityType accept, IdentityType type) { if (accept == type) { return true; } return false; }
//identity expression void VarPrimitive(YSParseNode VarPrimitiveNode, IdentityType IType) { Current = VarPrimitiveNode; YSToken NameToken = VarPrimitiveNode.Children[0].Children[0].Token; ExpectNonExistance(NameToken.Content); IDPacket primitive = IDPacket.CreateIDPacket(STATE, NameToken.Content, IType); Debug("Creating variable " + NameToken.Content + " of type " + IType); if (VarPrimitiveNode.Children.Count > 1) { IDPacket PEXP = Expression(VarPrimitiveNode.Children [1]); ExpectType(primitive.Type, PEXP.Type); STATE.COPY(primitive, PEXP); Debug("Assignment complete"); } else { switch (IType) { case IdentityType.Number: STATE.PutNumber(primitive, 0); break; case IdentityType.Boolean: STATE.PutBoolean(primitive, false); break; case IdentityType.Text: STATE.PutText(primitive, ""); break; } } Debug("Exit VarPrimitive"); }
bool AcceptType(IdentityType accept, IdentityType type) { if (accept == type) { return(true); } return(false); }
IDPacket IdentityFunction(YSParseNode IFunctionNode) { Debug("Attempting to execute function"); Current = IFunctionNode; string FunctionName = IFunctionNode.Children [0].Token.Content; IDPacket FunctionPacket = IDPacket.CreateIDPacket(STATE, FunctionName, IdentityType.Function); //execute the function FunctionFrame FT; if (!STATE.TryGetFunction(FunctionPacket, out FT)) { Error("Could not retreive function frame."); } //Create bindings List <IDPacket> Bindings = new List <IDPacket> (); foreach (YSParseNode arg in IFunctionNode.Children[1].Children) { IDPacket argexp; if (arg.Type == NType.Expression) { argexp = Expression(arg); } else if (arg.Type == NType.ExpressionList) { List <int> dims = new List <int> (); IdentityType ResolvedType = IdentityType.Unknown; argexp = ExpressionList(arg, ref dims, ref ResolvedType); argexp.ArrayType = ResolvedType; dims.Reverse(); argexp.TypeDimensions = dims.ToArray(); } else { Error("Expecting either an expression or an expression list"); argexp = IDPacket.CreateSystemPacket("TRASH", IdentityType.Unknown); } Bindings.Add(argexp); } ScopeFrame FunctionScope = STATE.CreateFunctionScope(FT, FunctionName, Bindings); FunctionScope.Name = FunctionName; FunctionScope.Type = ScopeFrame.FrameTypes.Function; STATE.PushScope(FunctionScope); YSParseNode FunctionBlockNode = FT.Block; Block(FunctionBlockNode); STATE.PopScopeNoSave(); Debug("Finished execution"); return(IDPacket.CreateReturnPacket(FT.Returns)); }
bool ExpectType(IdentityType expect, IdentityType type) { if (AcceptType(expect, type)) { return(true); } else { Error("Unexpected Type: " + type + " expecting " + expect); } return(false); }
bool DataType(YSParseNode DataTypeNode, out IdentityType Type, out List <int> Dimensions) { Debug("Beginning a Data Type"); if (DataTypeNode.Children.Count < 1) { Dimensions = null; Type = IdentityType.Unknown; return(false); } else if (DataTypeNode.Children.Count > 1) { Dimensions = new List <int> (); Type = STATE.TranslateTokenTypeToIdentityType(DataTypeNode.Children [1].Token.Type); if (DataTypeNode.Children.Count > 2 && DataTypeNode.Children [2].Type == NType.ArrayDimensions) { YSParseNode ADNode = DataTypeNode.Children [2]; foreach (YSParseNode Terminal in ADNode.Children) { if (Terminal.Token.Type != YSToken.TokenType.NumberData && Terminal.Token.Type != YSToken.TokenType.Asterisk) { Error("Expecting a number or *"); } if (Terminal.Token.Type == YSToken.TokenType.Asterisk) { Dimensions.Add(-1); } else { int dimval = int.Parse(Terminal.Token.Content); if (dimval < 0) { Error("Dimension values cannot be under 0"); } Dimensions.Add(dimval); } } } return(true); } else { Dimensions = null; Type = STATE.TranslateTokenTypeToIdentityType(DataTypeNode.Children [0].Token.Type); return(true); } Debug("Exiting Data Type"); }
//type VarPrimitive(identity expression) { VarPrimitive(identity expression) } void VarCreate(YSParseNode VarCreateNode) { Current = VarCreateNode; List <int> Dimensions; IdentityType _DataType; DataType(VarCreateNode.Children [0], out _DataType, out Dimensions); if (Dimensions != null) { YSToken NameToken = VarCreateNode.Children [1].Token; if (VarCreateNode.Children [2].Type == NType.ExpressionList) { IdentityType ResolvedType = IdentityType.Unknown; List <int> ReversedDimensions = new List <int> (); IDPacket exp_list = ExpressionList(VarCreateNode.Children [2], ref ReversedDimensions, ref ResolvedType); IDPacket put = IDPacket.CreateIDPacket(STATE, NameToken.Content, IdentityType.Structure); put.ArrayType = ResolvedType; ReversedDimensions.Reverse(); ArrayFrame AF = new ArrayFrame(put.ArrayType, ReversedDimensions.ToArray()); STATE.PutGeneric(put, AF); GenericFrame CastedGeneric; ArrayFrame AF_Casted; STATE.TryGetGeneric(put, out CastedGeneric); AF_Casted = (ArrayFrame)CastedGeneric; STATE.COPY(put, exp_list); Debug("T"); } else if (VarCreateNode.Children [2].Type == NType.ArrayInit) { //TODO array initializer } else { Error("Arrays must be initialized with an expression list or array initializer"); } } else { for (int i = 1; i < VarCreateNode.Children.Count; i++) { VarPrimitive(VarCreateNode.Children [i], _DataType); } } Debug("Exit VarCreate"); }
IDPacket Identity(YSParseNode INode) { Debug("Resolving basic identity"); Current = INode; switch (INode.Type) { case NType.Identity: YSParseNode IdentityNode = INode; YSParseNode TerminalNode = IdentityNode.Children [0]; IdentityType IdentityType = STATE.ResolveIdentityType(TerminalNode.Token); string IdentityName = TerminalNode.Token.Content; //get the IDPacket IDPacket ID = IDPacket.CreateIDPacket(STATE, IdentityName, IdentityType); ErrorIfUnknown("Exit ExpressionFactor (Identity)", ID); return(ID); case NType.IdentityArray: IDPacket AID = IdentityArray(INode); ErrorIfUnknown("Exit ExpressionFactor (Array)", AID); return(AID); case NType.IdentityFunction: IDPacket RID = IdentityFunction(INode); ErrorIfUnknown("Exit ExpressionFactor (Function)", RID); return(RID); case NType.IdentityStructure: //string StructureName = INode.Token.Content; //IDPacket SID = IDPacket.CreateIDPacket (STATE, StructureName, IdentityType.Structure); IDPacket SID = IdentityStructure(INode); ErrorIfUnknown("Exit ExpressionFactor (Structure)", SID); return(SID); default: Error("Identity could not be resolved"); return(IDPacket.CreateSystemPacket("", IdentityType.Unknown)); } }
//identity expression void VarPrimitive(YSParseNode VarPrimitiveNode, IdentityType IType) { Current = VarPrimitiveNode; YSToken NameToken = VarPrimitiveNode.Children[0].Children[0].Token; ExpectNonExistance (NameToken.Content); IDPacket primitive = IDPacket.CreateIDPacket (STATE, NameToken.Content, IType); Debug ("Creating variable " + NameToken.Content + " of type " + IType); if (VarPrimitiveNode.Children.Count > 1) { IDPacket PEXP = Expression (VarPrimitiveNode.Children [1]); ExpectType (primitive.Type, PEXP.Type); STATE.COPY (primitive, PEXP); Debug ("Assignment complete"); } else { switch (IType) { case IdentityType.Number: STATE.PutNumber (primitive, 0); break; case IdentityType.Boolean: STATE.PutBoolean (primitive, false); break; case IdentityType.Text: STATE.PutText (primitive, ""); break; } } Debug ("Exit VarPrimitive"); }
IDPacket ExpressionList(YSParseNode ExpressionListNode, ref List<int> DimensionsReversed, ref IdentityType ResolvedType) { DIMCNT = 0; Debug ("Expression List Resolving"); //STATE.PutStructure (ARRAY, ArrayFrame); if (ExpressionListNode.Children.Count < 1) { Debug ("Expression List had no children"); return null; } IDPacket ARRAY = IDPacket.CreateSystemPacket ("EXPL_TEMP", IdentityType.Structure); ScopeFrame ArrayScope = new ScopeFrame (new StructureFrame(), ARRAY.Name, ScopeFrame.FrameTypes.None); STATE.PushScope (ArrayScope); //List<IDPacket> RESULT = new List<IDPacket> (); //Look at the first node, all other nodes must conform to this node's type int NC = 0; YSParseNode FirstNode = ExpressionListNode.Children [NC++]; //IdentityType ListType; int ListDimensions = 0; if (FirstNode.Type == NType.ExpressionList) { ListDimensions++; IDPacket IEXPL = ExpressionList (FirstNode, ref DimensionsReversed, ref ResolvedType); DimensionsReversed.AddRange (new List<int>(DimensionsReversed)); //RESULT.Add (IEXPL); IDPacket FIRST = IDPacket.CreateIDPacket (STATE, "0", IdentityType.Structure); STATE.COPY (FIRST, IEXPL); } else { IDPacket EXP = Expression (FirstNode); ResolvedType = EXP.Type; if (!STATE.IsPrimitive (ResolvedType)) Error ("Expression Lists can only contain identities that resolve to Primitive Types"); //RESULT.Add (EXP); IDPacket FIRST = IDPacket.CreateIDPacket (STATE, "0", EXP.Type); STATE.COPY (FIRST, EXP); } int ELEMENT_COUNT = 1; while (NC < ExpressionListNode.Children.Count) { YSParseNode Node = ExpressionListNode.Children [NC++]; if (Node.Type != FirstNode.Type) Error (String.Format("All children in an expression list must have the same dimensions " + "Expecting {0} got {1}", FirstNode.Type, Node.Type)); if (FirstNode.Type == NType.ExpressionList) { IdentityType NodeType = IdentityType.Unknown; int NodeDimensions = 0; IDPacket NEXPL = ExpressionList (Node, ref DimensionsReversed, ref NodeType); if (NodeDimensions != ListDimensions) { Error (String.Format ("All children of an expression list must have matching dimensions. " + "Expected dimensions:{0} Found:{1}", ListDimensions, NodeDimensions)); } if (NodeType != ResolvedType) { Error (String.Format ("All children of an expression list must have matching types. " + "Expected type:{0} Found:{1}", ResolvedType, NodeType)); } //RESULT.Add (NEXPL); IDPacket NODE = IDPacket.CreateIDPacket (STATE, "" + ELEMENT_COUNT++, IdentityType.Structure); STATE.COPY (NODE, NEXPL); } else { IDPacket NEXP = Expression (Node); if (NEXP.Type != ResolvedType) { Error (String.Format ("All children of an expression list must have matching types. " + "Expected type:{0} Found:{1}", ResolvedType, NEXP.Type)); } //RESULT.Add (NEXP); IDPacket NODE = IDPacket.CreateIDPacket (STATE, "" + ELEMENT_COUNT++, NEXP.Type); STATE.COPY (NODE, NEXP); } } DIMCNT = ELEMENT_COUNT; DimensionsReversed.Add (DIMCNT); ArrayScope = STATE.PopScopeNoSave (); ArrayFrame ArrayFrame = new ArrayFrame (new IdentityType[] { ResolvedType }); ArrayFrame.Merge ((GenericFrame)ArrayScope); ArrayFrame.ResolvedType = ResolvedType; DimensionsReversed.Reverse (); ArrayFrame.Dimensions = DimensionsReversed.ToArray(); ARRAY.ArrayType = ResolvedType; STATE.PutGeneric (ARRAY, ArrayFrame); //ResolvedType = RESOLVED_TYPE; return ARRAY; }
bool ExpectType(IdentityType expect, IdentityType type) { if (AcceptType (expect, type)) { return true; } else { Error("Unexpected Type: " + type + " expecting " + expect); } return false; }
bool DataType(YSParseNode DataTypeNode, out IdentityType Type, out List<int> Dimensions) { Debug ("Beginning a Data Type"); if (DataTypeNode.Children.Count < 1) { Dimensions = null; Type = IdentityType.Unknown; return false; } else if (DataTypeNode.Children.Count > 1) { Dimensions = new List<int> (); Type = STATE.TranslateTokenTypeToIdentityType (DataTypeNode.Children [1].Token.Type); if (DataTypeNode.Children.Count > 2 && DataTypeNode.Children [2].Type == NType.ArrayDimensions) { YSParseNode ADNode = DataTypeNode.Children [2]; foreach (YSParseNode Terminal in ADNode.Children) { if (Terminal.Token.Type != YSToken.TokenType.NumberData && Terminal.Token.Type != YSToken.TokenType.Asterisk) Error ("Expecting a number or *"); if (Terminal.Token.Type == YSToken.TokenType.Asterisk) Dimensions.Add (-1); else { int dimval = int.Parse (Terminal.Token.Content); if (dimval < 0) Error ("Dimension values cannot be under 0"); Dimensions.Add (dimval); } } } return true; } else { Dimensions = null; Type = STATE.TranslateTokenTypeToIdentityType(DataTypeNode.Children [0].Token.Type); return true; } Debug ("Exiting Data Type"); }
IDPacket ExpressionList(YSParseNode ExpressionListNode, ref List <int> DimensionsReversed, ref IdentityType ResolvedType) { DIMCNT = 0; Debug("Expression List Resolving"); //STATE.PutStructure (ARRAY, ArrayFrame); if (ExpressionListNode.Children.Count < 1) { Debug("Expression List had no children"); return(null); } IDPacket ARRAY = IDPacket.CreateSystemPacket("EXPL_TEMP", IdentityType.Structure); ScopeFrame ArrayScope = new ScopeFrame(new StructureFrame(), ARRAY.Name, ScopeFrame.FrameTypes.None); STATE.PushScope(ArrayScope); //List<IDPacket> RESULT = new List<IDPacket> (); //Look at the first node, all other nodes must conform to this node's type int NC = 0; YSParseNode FirstNode = ExpressionListNode.Children [NC++]; //IdentityType ListType; int ListDimensions = 0; if (FirstNode.Type == NType.ExpressionList) { ListDimensions++; IDPacket IEXPL = ExpressionList(FirstNode, ref DimensionsReversed, ref ResolvedType); DimensionsReversed.AddRange(new List <int>(DimensionsReversed)); //RESULT.Add (IEXPL); IDPacket FIRST = IDPacket.CreateIDPacket(STATE, "0", IdentityType.Structure); STATE.COPY(FIRST, IEXPL); } else { IDPacket EXP = Expression(FirstNode); ResolvedType = EXP.Type; if (!STATE.IsPrimitive(ResolvedType)) { Error("Expression Lists can only contain identities that resolve to Primitive Types"); } //RESULT.Add (EXP); IDPacket FIRST = IDPacket.CreateIDPacket(STATE, "0", EXP.Type); STATE.COPY(FIRST, EXP); } int ELEMENT_COUNT = 1; while (NC < ExpressionListNode.Children.Count) { YSParseNode Node = ExpressionListNode.Children [NC++]; if (Node.Type != FirstNode.Type) { Error(String.Format("All children in an expression list must have the same dimensions " + "Expecting {0} got {1}", FirstNode.Type, Node.Type)); } if (FirstNode.Type == NType.ExpressionList) { IdentityType NodeType = IdentityType.Unknown; int NodeDimensions = 0; IDPacket NEXPL = ExpressionList(Node, ref DimensionsReversed, ref NodeType); if (NodeDimensions != ListDimensions) { Error(String.Format("All children of an expression list must have matching dimensions. " + "Expected dimensions:{0} Found:{1}", ListDimensions, NodeDimensions)); } if (NodeType != ResolvedType) { Error(String.Format("All children of an expression list must have matching types. " + "Expected type:{0} Found:{1}", ResolvedType, NodeType)); } //RESULT.Add (NEXPL); IDPacket NODE = IDPacket.CreateIDPacket(STATE, "" + ELEMENT_COUNT++, IdentityType.Structure); STATE.COPY(NODE, NEXPL); } else { IDPacket NEXP = Expression(Node); if (NEXP.Type != ResolvedType) { Error(String.Format("All children of an expression list must have matching types. " + "Expected type:{0} Found:{1}", ResolvedType, NEXP.Type)); } //RESULT.Add (NEXP); IDPacket NODE = IDPacket.CreateIDPacket(STATE, "" + ELEMENT_COUNT++, NEXP.Type); STATE.COPY(NODE, NEXP); } } DIMCNT = ELEMENT_COUNT; DimensionsReversed.Add(DIMCNT); ArrayScope = STATE.PopScopeNoSave(); ArrayFrame ArrayFrame = new ArrayFrame(new IdentityType[] { ResolvedType }); ArrayFrame.Merge((GenericFrame)ArrayScope); ArrayFrame.ResolvedType = ResolvedType; DimensionsReversed.Reverse(); ArrayFrame.Dimensions = DimensionsReversed.ToArray(); ARRAY.ArrayType = ResolvedType; STATE.PutGeneric(ARRAY, ArrayFrame); //ResolvedType = RESOLVED_TYPE; return(ARRAY); }