private HVMVariable GetVariable(SyntaxNode owner) { GetNextTokenWithTypeCheck(HVMTokenId.VarKeyword); HVMToken name = GetNextTokenWithTypeCheck(HVMTokenId.LiteralIdentifier); HVMType varType = GetDataType(); HVMVariable var = new HVMVariable(owner); var.VarType = varType; var.Name = name.Value; return(var); }
public SyntaxTree(List <HVMToken> Tokens, string source) { _ProgramNode = new SyntaxNode(null); this._Tokens = Tokens; this._Source = source; // Create tree from tokens. HVMToken token; while ((token = GetNextToken()) != null) { if (token.Id == HVMTokenId.FuncKeyword) { // The following forms of functions we expect. // func:<$>type name(argument-block) (code-block) // func name(argument-block) (code-block) - this has no return type (void) // func:type operator(argument-block) (code-block) - does this HAVE to have a type for all overloaddable operators? HVMType type = null; // It has a type identifier. if (PeekNextToken().Id == HVMTokenId.TypeIdentifier) { type = GetDataType(); } else // No type, assume void { type = new HVMType(); type.Type = HVMTypes.Void; } // No type identifier means it doesn't return anything. // This could also be an overloaddable operator too. HVMToken nameOrOperator = GetNextToken(); HVMFunc funcSignature = null; // Technically the owner is the program body. if (nameOrOperator.Id == HVMTokenId.LiteralIdentifier) { funcSignature = new HVMFunc(_ProgramNode); funcSignature.Name = nameOrOperator.Value; } else { funcSignature = new HVMOperatorFunc(_ProgramNode); foreach (HVMTokenId overloadable in _OverloadableOperations) { if (nameOrOperator.Id == overloadable) { (funcSignature as HVMOperatorFunc).Operator = nameOrOperator.Id; break; } } if ((funcSignature as HVMOperatorFunc).Operator == null) { ThrowSyntaxParseError(nameOrOperator, "Cannot overload that operator "); } } funcSignature.ReturnType = type; // Start checking for parameters. GetNextTokenWithTypeCheck(HVMTokenId.OpenParenthesis); if (PeekNextToken().Id == HVMTokenId.CloseParenthesis) { GetNextTokenWithTypeCheck(HVMTokenId.CloseParenthesis); // No parameters in the function. funcSignature.Parameters.Clear(); // Make sure it is empty. } else if (PeekNextToken().Id == HVMTokenId.LiteralIdentifier) // Parameters. { do { // Swollow the term seperator. if (PeekNextToken().Id == HVMTokenId.TermSeperator) { GetNextTokenWithTypeCheck(HVMTokenId.TermSeperator); } // Get the parameter name and type. HVMToken paramName = GetNextTokenWithTypeCheck(HVMTokenId.LiteralIdentifier); HVMType paramType = GetDataType(); // Function owns the parameters. HVMVariable parameter = new HVMVariable(funcSignature); parameter.Name = paramName.Value; parameter.VarType = paramType; funcSignature.Parameters.Add(parameter); } while (PeekNextToken().Id == HVMTokenId.TermSeperator); GetNextTokenWithTypeCheck(HVMTokenId.CloseParenthesis); } else { HVMToken wrongToken = GetNextToken(); ThrowSyntaxParseError(wrongToken, "Unexpected token in function signature " + wrongToken.Id + ", expected either parameters or ) "); } funcSignature.FunctionBody = new SyntaxNode(funcSignature); // Main function body code, currently not parsing. GetNextTokenWithTypeCheck(HVMTokenId.OpenParenthesis); GetNextTokenWithTypeCheck(HVMTokenId.CloseParenthesis); #if DEBUG if (funcSignature is HVMOperatorFunc) { Console.WriteLine((funcSignature as HVMOperatorFunc).ToString()); } else { Console.WriteLine(funcSignature.ToString()); } #endif // Add the function to the program. _ProgramNode.SubNodes.Add(funcSignature); } else if (token.Id == HVMTokenId.StructKeyword) // Parse struct. { HVMStruct newStruct = new HVMStruct(_ProgramNode); HVMToken name = GetNextTokenWithTypeCheck(HVMTokenId.LiteralIdentifier); newStruct.Name = name.Value; GetNextTokenWithTypeCheck(HVMTokenId.OpenParenthesis); // The members of the struct. do { // Swollow the term seperator. if (PeekNextToken().Id == HVMTokenId.TermSeperator) { GetNextTokenWithTypeCheck(HVMTokenId.TermSeperator); } // Get the parameter name and type. HVMToken paramName = GetNextTokenWithTypeCheck(HVMTokenId.LiteralIdentifier); HVMType paramType = GetDataType(); // Function owns the parameters. HVMVariable parameter = new HVMVariable(newStruct); parameter.Name = paramName.Value; parameter.VarType = paramType; newStruct.Parameters.Add(parameter); } while (PeekNextToken().Id == HVMTokenId.TermSeperator); GetNextTokenWithTypeCheck(HVMTokenId.CloseParenthesis); Console.WriteLine("Struct name=" + newStruct.Name + " parameters=" + newStruct.Parameters.Count); _ProgramNode.SubNodes.Add(newStruct); } } }