private static Token Expect(TokenParser parser, TokenParser.Tokens expectedToken) { EatWhiteSpace(parser); Token token = parser.GetToken(); if (token != null && token.TokenName != expectedToken) { Console.WriteLine("Expected Token " + expectedToken); Environment.Exit(0); } if (token == null) { Console.WriteLine("Unexpected end of input!"); Environment.Exit(0); } return(token); }
private static int GetPrecedence(TokenParser.Tokens token) { if (token == TokenParser.Tokens.Add || token == TokenParser.Tokens.Subtract) { return(1); } if (token == TokenParser.Tokens.Multiply || token == TokenParser.Tokens.Divide || token == TokenParser.Tokens.Modulus) { return(2); } if (token == TokenParser.Tokens.Exponent) { return(3); } if (token == TokenParser.Tokens.Lparen || token == TokenParser.Tokens.Rparen) { return(4); } return(0); }
private static bool IsOperator(TokenParser.Tokens token) { return(token == TokenParser.Tokens.Add || token == TokenParser.Tokens.Divide || token == TokenParser.Tokens.Exponent || token == TokenParser.Tokens.Modulus || token == TokenParser.Tokens.Multiply || token == TokenParser.Tokens.Subtract); }
public Token(TokenParser.Tokens name, string value) { TokenName = name; TokenValue = value; }
private string DoCompare(string leftOutput, string rightOutput, TokenParser.Tokens equalityToken, bool isString) { string output = ""; output = output + leftOutput + Environment.NewLine; if (isString) { //output = output + " ldy #compbuf1" + Environment.NewLine; //output = output + " call tmpstrcopy" + Environment.NewLine; } output = output + " tfr x,y" + Environment.NewLine; output = output + rightOutput + Environment.NewLine; if (isString) { //output = output + " ldy #compbuf2" + Environment.NewLine; //output = output + " call tmpstrcopy" + Environment.NewLine; } if (isString) { //output = output + " ldx #compbuf1" + Environment.NewLine; //output = output + " ldy #compbuf2" + Environment.NewLine; output = output + " push x" + Environment.NewLine; output = output + " push y" + Environment.NewLine; output = output + " call bufcomp" + Environment.NewLine; output = output + " tfr x,d" + Environment.NewLine; output = output + " pop x" + Environment.NewLine; output = output + " call freememory" + Environment.NewLine; output = output + " pop x" + Environment.NewLine; output = output + " call freememory" + Environment.NewLine; output = output + " tfr d,x" + Environment.NewLine; output = output + " cmpx #1" + Environment.NewLine; } else { output = output + " cmpy x" + Environment.NewLine; } string opcode = ""; switch (equalityToken) { case TokenParser.Tokens.EQUAL: opcode = "jeq"; break; case TokenParser.Tokens.NOTEQUAL: opcode = "jne"; break; case TokenParser.Tokens.LESSTHANOREQUAL: opcode = "jle"; break; case TokenParser.Tokens.LESSTHAN: opcode = "jlt"; break; case TokenParser.Tokens.GREATERTHENOREQUAL: opcode = "jge"; break; case TokenParser.Tokens.GREATERTHAN: opcode = "jgt"; break; } output = output + " " + opcode + " ifstate" + Output.LoopCounter + Environment.NewLine; output = output + " ldx #0" + Environment.NewLine; output = output + " jmp ifxit" + Output.LoopCounter + Environment.NewLine; output = output + "ifstate" + Output.LoopCounter + " ldx #1" + Environment.NewLine; output = output + "ifxit" + Output.LoopCounter + " nop" + Environment.NewLine; Output.LoopCounter++; return(output); }