private static bool Start() { globalOffset = 12;//first line in assembly is a jmp if (!semanticPass) { address = 0; offset = 0; memberVariables = new List <string>(); } while (ClassDeclaration()) { ; } if (!Tokens.GetToken().lexeme.Equals("void")) { SyntaxError(Tokens.GetToken(), "void return for main"); } Tokens.NextToken(); if (!Tokens.GetToken().lexeme.Equals("pxi")) { SyntaxError(Tokens.GetToken(), "pxi"); } Tokens.NextToken(); if (!Tokens.GetToken().lexeme.Equals("main")) { SyntaxError(Tokens.GetToken(), "main method"); } string[] data = new string[2]; data[0] = "returnType:void"; data[1] = "accessMod:public"; Symbol symbol = new Symbol("g", ("MAIN"), Tokens.GetToken().lexeme, "main", data); SymbolTable.Add(symbol); currentMethodName = "M"; identifierToken = Tokens.GetToken(); currentIdentifier = ""; scope += ".main"; if (semanticPass) { ICode.FUNC(symbol.symid); } Tokens.NextToken(); if (Tokens.GetToken().lexeme != "(") { SyntaxError(Tokens.GetToken(), "("); } Tokens.NextToken(); if (Tokens.GetToken().lexeme != ")") { SyntaxError(Tokens.GetToken(), ")"); } Tokens.NextToken(); if (!MethodBody()) { SyntaxError(Tokens.GetToken(), "method body"); } return(true); }
private static bool ConstructorDeclaration() { if (Tokens.GetToken().type != Token.Type.Identifier) { return(false); } currentIdentifier = Tokens.GetToken().lexeme; identifierToken = Tokens.GetToken(); if (semanticPass) { SemanticActions.dup(identifierToken, scope); SemanticActions.CD(identifierToken, scope); } Tokens.NextToken(); if (Tokens.GetToken().lexeme != "(") { SyntaxError(Tokens.GetToken(), "("); } Tokens.NextToken(); offset = 0; sizeParameters = 0; ParameterList(); string[] data = new string[2]; data[0] = "returnType:" + currentIdentifier; data[1] = "accessMod:" + accessMod; Symbol symbol = new Symbol(scope, ("X" + uniqueCounter++), currentIdentifier, "constructor", data); currentMethodName = symbol.symid; if (!semanticPass) { currentClassConstructorSymid = symbol.symid; } data[0] = "returnType:" + currentIdentifier; data[1] = "accessMod:" + accessMod; Symbol symbol2 = new Symbol(scope, ("Y" + symbol.symid.Substring(1)), currentIdentifier + "StaticInit", "Init", data); if (semanticPass) { ICode.RETURN("this"); ICode.FUNC(symbol.symid); ICode.FRAME(symbol2.symid, "this"); ICode.CALL(symbol2.symid); } symbol.parameters = parameters; if (Tokens.GetToken().lexeme != ")") { SyntaxError(Tokens.GetToken(), ")"); } Tokens.NextToken(); if (!MethodBody()) { SyntaxError(Tokens.GetToken(), "method body"); } symbol.size = methodSize; symbol2.size = 0; SymbolTable.Add(symbol); SymbolTable.Add(symbol2); offset = 0; methodSize = 0; return(true); }
private static bool FieldDeclaration() { if (Tokens.GetToken().lexeme == "(") { Tokens.NextToken(); parameters = ""; string methodType = currentType; offset = 0; sizeParameters = 0; ParameterList(); methodSize = 0; string[] data = new string[2]; data[0] = "returnType:" + methodType; data[1] = "accessMod:" + accessMod; Symbol symbol = new Symbol(scope, ("M" + uniqueCounter++), currentIdentifier, "method", data); symbol.parameters = parameters; currentMethodName = "M"; if (semanticPass) { ICode.FUNC(symbol.symid); } if (Tokens.GetToken().lexeme != ")") { SyntaxError(Tokens.GetToken(), ")"); } Tokens.NextToken(); if (!MethodBody()) { SyntaxError(Tokens.GetToken(), "method body"); } symbol.size = methodSize; SymbolTable.Add(symbol); offset = 0; methodSize = 0; return(true); } else { bool flag = false; if (Tokens.GetToken().lexeme == "[") { flag = true; if (Tokens.PeekToken().lexeme != "]") { return(false); } Tokens.NextToken(); Tokens.NextToken(); } if (semanticPass) { SemanticActions.vPush(identifierSymbol, identifierToken, scope); } if (Tokens.GetToken().lexeme == "=") { flag = true; if (semanticPass) { SemanticActions.oPush(Tokens.GetToken()); } Tokens.NextToken(); if (!AssignmentExpression()) { return(false); } } if ((Tokens.GetToken().lexeme == ";")) { if (semanticPass) { SemanticActions.EOE(); } Tokens.NextToken(); return(true); } if (flag) { SyntaxError(Tokens.GetToken(), ";"); } } return(false); }