public Parser(string fileName) { this.code = new Code(); this.fileName = fileName; this.lines = new List <string>(); this.symbolTable = new SymbolTable(); foreach (var line in System.IO.File.ReadAllLines(fileName)) { Regex.Replace(line, @"\s+", ""); var trimmedLine = line.Trim(); int index = line.IndexOf("/"); if (String.IsNullOrEmpty(trimmedLine) || trimmedLine[0] == '/') { continue; } trimmedLine = index != -1 ? trimmedLine.Split('/')[0] : trimmedLine; if (line[0] == '(') { var key = line.Substring(1, line.Length - 2); symbolTable.AddEntry(key, lines.Count); System.Console.WriteLine("continue is called" + key); continue; } lines.Add(trimmedLine); } this.currentLine = 0; this.n = 16; foreach (var line in lines) { var currentSymbol = line.Substring(1, line.Length - 1); System.Console.WriteLine(currentSymbol); if (line[0] == '@' && !symbolTable.Contains(currentSymbol) && !int.TryParse(currentSymbol, out _)) { symbolTable.AddEntry(currentSymbol, n++); } } }
// parses one function definition void matchFunction() { match(Token.KwdFunc); currFunction = new CompiledFunction(sc.TokenText); // Get the function text string fnName = sc.TokenText; match(Token.Ident); // the function name // check for uniqueness, then add to the function list if unique. checkUniqueFunction(fnName); FunctionList.Add(fnName); currSymbolTable = new SymbolTable(); match(Token.LPar); if (nextToken != Token.RPar) { for( ; ; ) { string argname = sc.TokenText; match(Token.Ident); if (!currSymbolTable.AddEntry(argname, currFunction.AddArgument(argname))) SemanticError("duplicate declaration of argument {0}", argname); if (nextToken != Token.Comma) break; advance(); } } match(Token.RPar); matchBlock(); // the function should end with a return statement. In case ... currFunction.AddInstruction(OpCode.Stop); program.Add(currFunction); currFunction = null; currSymbolTable = null; }
// parses one function definition void matchFunction() { match(Token.KwdFunc); currFunction = new CompiledFunction(sc.TokenText); // Add next token to list string fn = sc.TokenText; int args = 0; Function func = new Function(fn, 0); if (!functionNames.Contains(fn)) functionNames.Add(fn); else throw new ParseError("Duplicate function name " + func.name); match(Token.Ident); // the function name currSymbolTable = new SymbolTable(); match(Token.LPar); if (nextToken != Token.RPar) { for( ; ; ) { string argname = sc.TokenText; args++; match(Token.Ident); if (!currSymbolTable.AddEntry(argname, currFunction.AddArgument(argname))) SemanticError("duplicate declaration of argument {0}", argname); if (nextToken != Token.Comma) break; advance(); } } func.args = args; functions.Add(func); match(Token.RPar); matchBlock(); // the function should end with a return statement. In case ... currFunction.AddInstruction(OpCode.Stop); program.Add(currFunction); currFunction = null; currSymbolTable = null; }