Permalink
Browse files

AdventureSample - support INPUT

  • Loading branch information...
bobbymcr committed Apr 23, 2018
1 parent c5df948 commit a1fab6a1542391dd48e049b1148baf8723a322ac
@@ -90,7 +90,10 @@ public void IfThen(BasicExpression cond, BasicStatement ifTrue)
public void Input(string prompt, BasicExpression v)
{
throw new NotImplementedException();
ExpressionNode expr = new ExpressionNode(this.generator, this.vars, this.methods);
v.Accept(expr);
SyntaxNode lval = expr.Value;
this.AddInput(prompt, lval);
}
public void Many(string name, BasicExpression[] list)
@@ -249,6 +252,46 @@ private void AddPrint(SyntaxNode arg, bool lineBreak)
this.methods.Add(name, printMethod);
}
private void AddInput(string prompt, SyntaxNode lval)
{
string name = "INPUT_n";
var rval = this.generator.InvocationExpression(this.generator.IdentifierName(name), this.generator.LiteralExpression(prompt));
this.lines.Add(this.lineNumber, this.generator.AssignmentStatement(lval, rval));
var output = this.generator.MemberAccessExpression(this.generator.ThisExpression(), this.generator.IdentifierName("output"));
var callWrite = this.generator.MemberAccessExpression(output, "Write");
var promptQ = this.generator.AddExpression(this.generator.IdentifierName("prompt"), this.generator.LiteralExpression("? "));
var writePrompt = this.generator.InvocationExpression(callWrite, promptQ);
var input = this.generator.MemberAccessExpression(this.generator.ThisExpression(), this.generator.IdentifierName("input"));
var callReadLine = this.generator.MemberAccessExpression(input, "ReadLine");
var read = this.generator.InvocationExpression(callReadLine);
var assignStr = this.generator.LocalDeclarationStatement(this.generator.TypeExpression(SpecialType.System_String), "v", read);
var declR = this.generator.LocalDeclarationStatement(this.generator.TypeExpression(SpecialType.System_Single), "r");
var callTryParse = this.generator.MemberAccessExpression(this.generator.TypeExpression(SpecialType.System_Single), "TryParse");
var argR = this.generator.Argument(RefKind.Out, this.generator.IdentifierName("r"));
var tryParse = this.generator.InvocationExpression(callTryParse, this.generator.IdentifierName("v"), argR);
var retR = this.generator.ReturnStatement(this.generator.IdentifierName("r"));
var ifParse = this.generator.IfStatement(tryParse, new SyntaxNode[] { retR });
var callWriteLine = this.generator.MemberAccessExpression(output, "WriteLine");
var writeError = this.generator.InvocationExpression(callWriteLine, this.generator.LiteralExpression("?Redo from start"));
SyntaxNode[] block = new SyntaxNode[]
{
writePrompt,
assignStr,
declR,
ifParse,
writeError
};
SyntaxNode[] inputStatements = new SyntaxNode[]
{
this.generator.WhileStatement(this.generator.LiteralExpression(true), block)
};
SyntaxNode[] parameters = new SyntaxNode[] { this.generator.ParameterDeclaration("prompt", type: this.generator.TypeExpression(SpecialType.System_String)) };
var ret = this.generator.TypeExpression(SpecialType.System_Single);
var inputMethod = this.generator.MethodDeclaration(name, returnType: ret, accessibility: Accessibility.Private, parameters: parameters, statements: inputStatements);
this.methods.Add(name, inputMethod);
}
private void AddReturn()
{
this.lines.AddReturn(this.lineNumber);
@@ -30,6 +30,7 @@ 140 CLS
160 GOSUB 2000
170 GOSUB 2020
180 GOSUB 2030
190 INPUT A
1000 GOTO 20
2000 CLS
2010 RETURN
@@ -105,6 +106,22 @@ private void PRINT_n(string expression)
this.output.Write(expression);
}
private float INPUT_n(string prompt)
{
while (true)
{
this.output.Write((prompt) + (""? ""));
string v = this.input.ReadLine();
float r;
if (float.TryParse(v, out r))
{
return r;
}
this.output.WriteLine(""?Redo from start"");
}
}
private int Sub_2000()
{
CLS();
@@ -173,6 +190,7 @@ private int Main()
return 2;
}
A_n = (INPUT_n(""""));
goto L20;
return 2;
}

0 comments on commit a1fab6a

Please sign in to comment.