public STObject(STClass @class) { Class = @class; InstanceContext = new LocalContext(GlobalContext.Instance, true); InstanceContext.Declare("self"); InstanceContext.SetVariable("self", this); }
public override STObject Invoke(STMessage message) { try { var instanceCtx = message.Receiver.InstanceContext; var invocationCtx = new LocalContext (instanceCtx); for (int i = 0, max = Prototype.ParameterNames.Length; i < max; ++i) invocationCtx.SetVariable(Prototype.ParameterNames[i].Name, message.Parameters[i]); return Block.EvaluateWith(invocationCtx); } catch (ReturnException e) { return e.Value; } }
public STBlock(Node blockLiteral, Context context, Compiler compiler) { Compiler = compiler; OuterContext = context; Context = new LocalContext(context, true); BlockLiteral = blockLiteral; BlockArgumentNames = new string[0]; LocalVariableNames = new string[0]; int i = 1; Node blockParams = null; if (blockLiteral[i].Name == "block_params") blockParams = blockLiteral[i++]; if (blockLiteral[i].Name == "sequence") { Sequence = blockLiteral[i++]; if (Sequence[0].Name == "var_def") { List<string> varNames = new List<string>(); var vardef = Sequence[0]; for (int j = 1, max = vardef.Count; j < max; ++j) { if (vardef[j].Name == "VAR_DELIM") break; var varName = (vardef[j] as Token).Image; varNames.Add (varName); Context.Declare(varName); } LocalVariableNames = varNames.ToArray(); } } if (blockParams != null) { var parms = blockParams; List<string> parmNames = new List<string>(); for (int j = 0, max = parms.Count; j < max; ++j) { var child = parms[j]; if (child.Name == "VAR_DELIM") break; var parmName = (parms[++j] as Token).Image; parmNames.Add (parmName); Context.Declare(parmName); } BlockArgumentNames = parmNames.ToArray(); } }
public static void Main(string[] args) { var interp = new Compiler(Assembly.GetCallingAssembly()); var ctx = new LocalContext(GlobalContext.Instance); while (true) { string input = ReadLine.readline("ist: "); if (input.StartsWith("#include ")) { using (var fr = new StreamReader(input.Substring("#include ".Length))) input = fr.ReadToEnd(); } if (input == "exit") break; try { Evaluate (interp, input, ctx); } catch (ParseException e) { Console.WriteLine ("Parse error: " + e.Message); } catch (CompileException e) { Console.WriteLine ("Error compiling: " + e.Message); } catch (Exception e) { Console.WriteLine ("Unhandled exception: " + e); } } }