Permalink
Browse files

AdventureSample - add numeric variables

  • Loading branch information...
bobbymcr committed Apr 21, 2018
1 parent 06ba710 commit b36b6434caff3fbe4e77a398c9ae61c98adf6ef3
@@ -209,11 +209,13 @@ private sealed class Variables
{
private readonly SyntaxGenerator generator;
private readonly Dictionary<string, Variable> strs;
private readonly Dictionary<string, Variable> nums;
public Variables(SyntaxGenerator generator)
{
this.generator = generator;
this.strs = new Dictionary<string, Variable>();
this.nums = new Dictionary<string, Variable>();
}
public IEnumerable<SyntaxNode> Fields()
@@ -222,6 +224,11 @@ public IEnumerable<SyntaxNode> Fields()
{
yield return v.Field(this.generator);
}
foreach (Variable v in this.nums.Values)
{
yield return v.Field(this.generator);
}
}
public SyntaxNode Init()
@@ -232,16 +239,33 @@ public SyntaxNode Init()
statements.Add(v.Init(this.generator));
}
foreach (Variable v in this.nums.Values)
{
statements.Add(v.Init(this.generator));
}
return this.generator.MethodDeclaration("Init", accessibility: Accessibility.Private, statements: statements);
}
public SyntaxNode Add(BasicType type, string name)
{
Variable v;
if (!this.strs.TryGetValue(name, out v))
if (type == BasicType.Str)
{
v = new Variable(type, name);
this.strs.Add(name, v);
if (!this.strs.TryGetValue(name, out v))
{
v = new Variable(type, name);
this.strs.Add(name, v);
}
}
else
{
if (!this.nums.TryGetValue(name, out v))
{
v = new Variable(type, name);
this.nums.Add(name, v);
}
}
return v.Ref(this.generator);
@@ -258,7 +282,19 @@ public Variable(BasicType type, string name)
this.name = name;
}
public string Name => this.name + "_s";
public string Name
{
get
{
string suffix = "_n";
if (this.type == BasicType.Str)
{
suffix = "_s";
}
return this.name + suffix;
}
}
public SyntaxNode Ref(SyntaxGenerator generator)
{
@@ -267,13 +303,34 @@ public SyntaxNode Ref(SyntaxGenerator generator)
public SyntaxNode Field(SyntaxGenerator generator)
{
var type = generator.TypeExpression(SpecialType.System_String);
return generator.FieldDeclaration(this.Name, type, accessibility: Accessibility.Private);
return generator.FieldDeclaration(this.Name, this.Type(generator), accessibility: Accessibility.Private);
}
public SyntaxNode Init(SyntaxGenerator generator)
{
return generator.AssignmentStatement(this.Ref(generator), generator.LiteralExpression(string.Empty));
return generator.AssignmentStatement(this.Ref(generator), this.Default(generator));
}
private SyntaxNode Default(SyntaxGenerator generator)
{
object lit = 0;
if (this.type == BasicType.Str)
{
lit = string.Empty;
}
return generator.LiteralExpression(lit);
}
private SyntaxNode Type(SyntaxGenerator generator)
{
SpecialType ty = SpecialType.System_Single;
if (this.type == BasicType.Str)
{
ty = SpecialType.System_String;
}
return generator.TypeExpression(ty);
}
}
}
@@ -17,6 +17,7 @@ public void FullClass()
30 A$=""a string""
40 A$=""same string""
50 B1$=""new string""
60 A=2
100 GOTO 20";
const string Expected = @"using System;
using System.IO;
@@ -27,6 +28,7 @@ internal sealed class MyProg
private readonly TextWriter output;
private string A_s;
private string B1_s;
private float A_n;
public MyProg(TextReader input, TextWriter output)
{
this.input = (input);
@@ -44,6 +46,7 @@ private void Init()
{
A_s = ("""");
B1_s = ("""");
A_n = (0);
}
private void PRINT(string expression)
@@ -61,6 +64,7 @@ private bool Main()
A_s = (""a string"");
A_s = (""same string"");
B1_s = (""new string"");
A_n = (2);
goto L20;
return false;
}

0 comments on commit b36b643

Please sign in to comment.