private static void Main(string[] args) { //args = new[] { @"C:\Users\osina\source\repos\Gnosis\Example Scripts\XnO.gno" }; if (args.Length == 0) { return; //exit program if no arguments } Lexer lexer = new Lexer(File.ReadAllText(args[0])); // Get tokens from code file Methods = lexer.Methods; globalVariableHandler = new VariableHandler(true); // variable handler for "main" Method mainMethod = lexer.MainMethod(); // Main method (entry point "main") // Create intepreter instance MethodHandler methodHandler = new MethodHandler(null, mainMethod); if (mainMethod != null) { methodHandler.DoFunction(mainMethod); // Inteprete commands in Main } }
public void IntepreteCommand(Statement statement) { CleanForArray(statement); if (statement.tokens.Count == 0) { return; } else if (statement.tokens[0] == "print") { Print(statement); } else if (statement.tokens[0] == "input") { Input(statement); } else if (statement.tokens[0] == "var") { VarDeclaration(statement); } else if (statement.tokens[0] == "if") { IfStatement(statement, false); } else if (statement.tokens[0] == "while") { IfStatement(statement, true); } else if (statement.tokens[0] == "for") { ForStatement(statement); } else if (statement.tokens[0] == "return") { var valueType = valueHanlder.ValueType(statement.tokens[1]); int lem = statement.tokens.Count; returned = ExpressStatement(statement.tokens.GetRange(1, lem - 2), valueType, false); return; } else if (IsVariable(statement.tokens[0])) { ShortHandStatement(statement); } else if (IsArrayPointer(statement.tokens[0])) { ArrayValueChangeStatement(statement); } else if (statement.tokens.Count == 2) { //If single statement like "pause" string command = statement.tokens[0]; if (command == "cls") { Console.Clear(); } if (command == "pause") { Console.WriteLine("Press any key to continue..."); Console.ReadKey(); } else if (EntryPoint.Methods.ContainsKey(command)) { Method m = EntryPoint.Methods[command]; MethodHandler mh = new MethodHandler(OuterVariables, m); mh.DoFunction(m); } } }
public dynamic GetValue(string value) { if (value.StartsWith("$")) { string val = value.Substring(1, value.Length - 1); if (IsArray(val)) { List <object> _ = new List <object>(); if (InnerVariables.IsArray(val)) { _ = (List <object>)method.GetVariable(val).value.value; } else if (OuterVariables.IsArray(val)) { _ = (List <object>)OuterVariables.GetVariable(val).value.value; } else if (GlobalVariables.IsArray(val)) { _ = (List <object>)GlobalVariables.GetVariable(val).value.value; } return(_.Count); } else { string rawValue = Convert.ToString(GetValue(val)); return(rawValue.Length); } } else if (value.Contains("[")) { //elements[0] var t = value.Split('[', ']'); var vName = t[0]; var index = Convert.ToInt32(GetValue(t[1])); if (!IsArray(vName)) { return(GetValue(vName)[index]); } else { return(GetArray(vName, index)); } } else if (Lexer.IsString(value)) { return(value.Substring(1, value.Length - 2) .Replace("\\a", "\a") .Replace("\\b", "\b") .Replace("\\f", "\f") .Replace("\\n", "\n") .Replace("\\r", "\r") .Replace("\\t", "\t") .Replace("\\v", "\v")); } else if (InnerVariables.IsVariable(value)) { return(method.GetVariable(value).value.value); } else if (OuterVariables != null && OuterVariables.IsVariable(value)) { return(OuterVariables.GetVariable(value).value.value); } else if (GlobalVariables.IsVariable(value)) { return(GlobalVariables.GetVariable(value).value.value); } else if (EntryPoint.Methods.ContainsKey(value)) { Method m = EntryPoint.Methods[value]; MethodHandler mh = new MethodHandler(null, m); mh.DoFunction(m); return(mh.returned); } else if (Value.IsNumber(value)) { return(value); } else { //To be implemented //Throw exception return(null); } }