public ReadCommand(TLine line, NBInterpreter i) : base(line, i) { KwRead read = (KwRead)line.Statement; if (read.Variable is NumVariable || read.Variable is NumArray) { IVariable <double> doubleVar = (IVariable <double>)Interpreter.Make(line.Clone((Statement)read.Variable)); _worker = c => { string next = Interpreter.ReadNextString(); if (next == null) { throw new InterpreterException(line, "READ overflow! Variable: " + doubleVar.Name); } double val; if (!double.TryParse(next, out val)) { throw new InterpreterException(line, "READ error! Variable: " + doubleVar.Name + " value: " + next); } doubleVar.Execute(c); doubleVar.SetValue(val); _read = val.ToString(); return(InterpreterStatus.Ok); }; } else { IVariable <string> stringVar = (IVariable <string>)Interpreter.Make(line.Clone((Statement)read.Variable)); _worker = c => { string next = Interpreter.ReadNextString(); if (next == null) { throw new InterpreterException(line, "READ overflow! Variable: " + stringVar.Name); } stringVar.Execute(c); stringVar.SetValue(next); _read = next; return(InterpreterStatus.Ok); }; } }
public InputCommand(TLine pg, NBInterpreter interpreter) : base(pg, interpreter) { string prompt = null; bool questionMark = true; KwInput i = (KwInput)pg.Statement; if (i.Prompt != null) { prompt = i.Prompt.Value; } if (i.Separator.HasValue && i.Separator.Value == ',') { questionMark = false; } _varName = i.Variable.Name; if (i.Variable is NumVariable || i.Variable is NumArray) { IVariable <double> variable = (IVariable <double>)Interpreter.Make(pg.Clone((Statement)i.Variable)); _worker = context => { variable.Execute(context); variable.SetValue(BNLib.InputDouble(context, prompt, questionMark)); }; } else { IVariable <string> variable = (IVariable <string>)Interpreter.Make(pg.Clone((Statement)i.Variable)); _worker = context => { variable.Execute(context); variable.SetValue(BNLib.InputString(context, prompt, questionMark)); }; } }