private static void Execute(string code, string fname, Context context, bool inlineOutput) { Tokenizer tokenizer = new Tokenizer(fname, code); TokenizerResult tokenizerResult = tokenizer.GenerateTokens(); if (!tokenizerResult.HasError) { Parser parser = new Parser(tokenizerResult.Tokens); ParserResult parserResult = parser.Parse(); if (!parserResult.HasError) { Interpreter interpreter = new Interpreter(); RuntimeResult runtimeResult = interpreter.Visit(parserResult.Node, context); if (!runtimeResult.HasError) { if (inlineOutput) { Console.WriteLine(runtimeResult.Value); } } else { Console.WriteLine(runtimeResult.Error); } } else { Console.WriteLine(parserResult.Error); } } else { Console.WriteLine(tokenizerResult.Error); } }
private RuntimeResult VisitLoadNode(LoadNode node, Context context) { string fname = node.Token.Value.ToString(); string fileName = ""; if (File.Exists(Program.FileDirectory + fname + Program.LibraryExt)) { fileName = Program.FileDirectory + fname + Program.LibraryExt; } else if (File.Exists(Program.FileDirectory + "libs\\" + fname + Program.LibraryExt)) { fileName = Program.FileDirectory + "libs\\" + fname + Program.LibraryExt; } else if (File.Exists(Program.FileDirectory + fname + ".si")) { fileName = Program.FileDirectory + fname + ".si"; } else if (File.Exists(Program.FileDirectory + "libs\\" + fname + ".si")) { fileName = Program.FileDirectory + "libs\\" + fname + ".si"; } else if (File.Exists(Program.BaseDirectory + fname + Program.LibraryExt)) { fileName = Program.BaseDirectory + fname + Program.LibraryExt; } else if (File.Exists(Program.BaseDirectory + "libs\\" + fname + Program.LibraryExt)) { fileName = Program.BaseDirectory + fname + "libs\\" + Program.LibraryExt; } else if (File.Exists(Program.BaseDirectory + fname + ".si")) { fileName = Program.BaseDirectory + fname + ".si"; } else if (File.Exists(Program.BaseDirectory + "libs\\" + fname + ".si")) { fileName = Program.BaseDirectory + "libs\\" + fname + ".si"; } if (fileName.Equals("")) { return(new RuntimeResult(new RuntimeError(node.Token.Position, "Assembly '" + fname + "' not found!", context))); } if (fileName.EndsWith(Program.LibraryExt)) { if (node.ClassToken != null) { Values.AssemblyValue value = new Values.AssemblyValue(); value.SetPositionAndContext(node.Token.Position, context); value.LoadAsm("libs\\" + fname + Program.LibraryExt, fname, node.ClassToken.Value.ToString()); context.AddSymbol(node.ClassToken.Value.ToString(), value); return(new RuntimeResult(value)); } else { //todo fix this system Assembly asm = Assembly.LoadFile(fileName); Type[] types = asm.GetTypes(); for (int i = 0; i < types.Length; i++) { Console.WriteLine(types[i].Name); } for (int i = 0; i < types.Length; i++) { Values.AssemblyValue value = new Values.AssemblyValue(); value.Assembly = asm; value.AsmType = types[i]; FieldInfo[] fields = types[i].GetFields(); for (int j = 0; j < fields.Length; j++) { context.AddSymbol(fields[j].Name, Values.AssemblyValue.ParseValue(fields[j].GetValue(null), node.Token.Position, context)); } value.SetPositionAndContext(node.Token.Position, context); context.AddSymbol(types[i].Name, value); if (i == types.Length - 1) { return(new RuntimeResult(value)); } } } } else { string code = File.ReadAllText(fileName).Replace("\r\n", "\n"); Tokenizer tokenizer = new Tokenizer(fname, code); TokenizerResult tokenizerResult = tokenizer.GenerateTokens(); if (!tokenizerResult.HasError) { Parser parser = new Parser(tokenizerResult.Tokens); ParserResult parserResult = parser.Parse(); if (!parserResult.HasError) { Interpreter interpreter = new Interpreter(); return(interpreter.Visit(parserResult.Node, context)); } else { return(new RuntimeResult(parserResult.Error)); } } else { return(new RuntimeResult(tokenizerResult.Error)); } } return(new RuntimeResult(new RuntimeError(node.Token.Position, "Library " + fname + " not found!", context))); }
public TokenizerResult GenerateTokens() { List <Token> tokens = new List <Token>(); while (currentChar != '\0') { if (SkipChars.Contains(currentChar)) { Advance(); } else if (currentChar == '0') { if (Peek() == 'x' || Peek() == 'X') { TokenizerResult result = MakeHexNumber(); if (result.HasError) { return(result); } tokens.Add(result.SingleToken); } else if (Peek() == 'o' || Peek() == 'O') { TokenizerResult result = MakeOctNumber(); if (result.HasError) { return(result); } tokens.Add(result.SingleToken); } else if (Peek() == 'b' || Peek() == 'B') { TokenizerResult result = MakeBinNumber(); if (result.HasError) { return(result); } tokens.Add(result.SingleToken); } else { TokenizerResult result = MakeNumber(); if (result.HasError) { return(result); } tokens.Add(result.SingleToken); } } else if (Digits.Contains(currentChar)) { TokenizerResult result = MakeNumber(); if (result.HasError) { return(result); } tokens.Add(result.SingleToken); } else if (currentChar == 'b' || currentChar == 'B') { if (Peek() == '\'' || Peek() == '"') { Advance(); Token strToken = MakeString(currentChar); tokens.Add(new Token(TokenType.BYTE_ARRAY, strToken.Value).SetPosition(strToken.Position)); } else { tokens.Add(MakeIdentifier()); } } else if (Letters.Contains(currentChar)) { tokens.Add(MakeIdentifier()); } else if (currentChar == '"') { tokens.Add(MakeString('"')); } else if (currentChar == '\'') { tokens.Add(MakeString('\'')); } else if (currentChar == '.') { tokens.Add(new Token(TokenType.DOT).SetPosition(currentPosition.Clone())); Advance(); } else if (currentChar == ':') { tokens.Add(new Token(TokenType.COLON).SetPosition(currentPosition.Clone())); Advance(); } else if (currentChar == '\n' || currentChar == ';') { tokens.Add(new Token(TokenType.NEWLINE).SetPosition(currentPosition.Clone())); Advance(); } else if (currentChar == '+') { if (Peek() == '=') { tokens.Add(new Token(TokenType.PLS_EQ).SetPosition(currentPosition.Clone())); Advance(2); } else { tokens.Add(new Token(TokenType.PLUS).SetPosition(currentPosition.Clone())); Advance(); } } else if (currentChar == '-') { if (Peek() == '=') { tokens.Add(new Token(TokenType.MIN_EQ).SetPosition(currentPosition.Clone())); Advance(2); } else { tokens.Add(new Token(TokenType.MINUS).SetPosition(currentPosition.Clone())); Advance(); } } else if (currentChar == '*') { if (Peek() == '*') { if (Peek(2) == '=') { tokens.Add(new Token(TokenType.EXP_EQ).SetPosition(currentPosition.Clone())); Advance(3); } else { tokens.Add(new Token(TokenType.EXPONENT).SetPosition(currentPosition.Clone())); Advance(2); } } else if (Peek() == '=') { tokens.Add(new Token(TokenType.MUL_EQ).SetPosition(currentPosition.Clone())); Advance(2); } else { tokens.Add(new Token(TokenType.MULTIPLY).SetPosition(currentPosition.Clone())); Advance(); } } else if (currentChar == '/') { if (Peek() == '=') { tokens.Add(new Token(TokenType.DIV_EQ).SetPosition(currentPosition.Clone())); Advance(2); } else if (Peek() == '/') { while (currentChar != '\n') { if (currentChar == '\0') { break; } Advance(); } Advance(); continue; } else if (Peek() == '*') { SkipMultilineComment(); } else { tokens.Add(new Token(TokenType.DIVIDE).SetPosition(currentPosition.Clone())); Advance(); } } else if (currentChar == '%') { if (Peek() == '=') { tokens.Add(new Token(TokenType.MOD_EQ).SetPosition(currentPosition.Clone())); Advance(2); } else { tokens.Add(new Token(TokenType.MODULUS).SetPosition(currentPosition.Clone())); Advance(); } } else if (currentChar == '&') { if (Peek() == '=') { tokens.Add(new Token(TokenType.AND_EQ).SetPosition(currentPosition.Clone())); Advance(2); } else if (Peek() == '&') { tokens.Add(new Token(TokenType.BOOLEAN_AND).SetPosition(currentPosition.Clone())); Advance(2); } else { tokens.Add(new Token(TokenType.BITWISE_AND).SetPosition(currentPosition.Clone())); Advance(); } } else if (currentChar == '|') { if (Peek() == '=') { tokens.Add(new Token(TokenType.OR_EQ).SetPosition(currentPosition.Clone())); Advance(2); } else if (Peek() == '|') { tokens.Add(new Token(TokenType.BOOLEAN_OR).SetPosition(currentPosition.Clone())); Advance(2); } else { tokens.Add(new Token(TokenType.BITWISE_OR).SetPosition(currentPosition.Clone())); Advance(); } } else if (currentChar == '^') { if (Peek() == '=') { tokens.Add(new Token(TokenType.XOR_EQ).SetPosition(currentPosition.Clone())); Advance(2); } else { tokens.Add(new Token(TokenType.BITWISE_XOR).SetPosition(currentPosition.Clone())); Advance(); } } else if (currentChar == '!') { if (Peek() == '=') { tokens.Add(new Token(TokenType.NOT_EQUALS).SetPosition(currentPosition.Clone())); Advance(2); } else { tokens.Add(new Token(TokenType.BOOLEAN_NOT).SetPosition(currentPosition.Clone())); Advance(); } } else if (currentChar == '~') { if (Peek() == '=') { tokens.Add(new Token(TokenType.COM_EQ).SetPosition(currentPosition.Clone())); Advance(2); } else { tokens.Add(new Token(TokenType.COMPLEMENT).SetPosition(currentPosition.Clone())); Advance(); } } else if (currentChar == '<') { if (Peek() == '=') { tokens.Add(new Token(TokenType.LESS_TOE).SetPosition(currentPosition.Clone())); Advance(2); } else if (Peek() == '<') { if (Peek(2) == '=') { tokens.Add(new Token(TokenType.LS_EQ).SetPosition(currentPosition.Clone())); Advance(3); } else { tokens.Add(new Token(TokenType.LEFT_SHIFT).SetPosition(currentPosition.Clone())); Advance(2); } } else { tokens.Add(new Token(TokenType.LESS_THAN).SetPosition(currentPosition.Clone())); Advance(); } } else if (currentChar == '>') { if (Peek() == '=') { tokens.Add(new Token(TokenType.GREATER_TOE).SetPosition(currentPosition.Clone())); Advance(2); } else if (Peek() == '>') { if (Peek(2) == '=') { tokens.Add(new Token(TokenType.RS_EQ).SetPosition(currentPosition.Clone())); Advance(3); } else { tokens.Add(new Token(TokenType.RIGHT_SHIFT).SetPosition(currentPosition.Clone())); Advance(2); } } else { tokens.Add(new Token(TokenType.GREATER_THAN).SetPosition(currentPosition.Clone())); Advance(); } } else if (currentChar == '=') { if (Peek() == '=') { tokens.Add(new Token(TokenType.EQUALS_EQUALS).SetPosition(currentPosition.Clone())); Advance(2); } else { tokens.Add(new Token(TokenType.EQUALS).SetPosition(currentPosition.Clone())); Advance(); } } else if (currentChar == '(') { tokens.Add(new Token(TokenType.LEFT_PAREN).SetPosition(currentPosition.Clone())); Advance(); } else if (currentChar == ')') { tokens.Add(new Token(TokenType.RIGHT_PAREN).SetPosition(currentPosition.Clone())); Advance(); } else if (currentChar == '[') { tokens.Add(new Token(TokenType.LEFT_SQB).SetPosition(currentPosition.Clone())); Advance(); } else if (currentChar == ']') { tokens.Add(new Token(TokenType.RIGHT_SQB).SetPosition(currentPosition.Clone())); Advance(); } else if (currentChar == '{') { tokens.Add(new Token(TokenType.LEFT_BRA).SetPosition(currentPosition.Clone())); Advance(); } else if (currentChar == '}') { tokens.Add(new Token(TokenType.RIGHT_BRA).SetPosition(currentPosition.Clone())); Advance(); } else if (currentChar == ',') { tokens.Add(new Token(TokenType.COMMA).SetPosition(currentPosition.Clone())); Advance(); } else { return(new TokenizerResult(new InvalidCharError(currentPosition, currentChar.ToString()))); } } tokens.Add(new Token(TokenType.EOF).SetPosition(currentPosition.Clone())); return(new TokenizerResult(tokens)); }