Permalink
Browse files

AdventureSample - support 1-D numeric arrays

  • Loading branch information...
bobbymcr committed Apr 21, 2018
1 parent ef9ef91 commit a8ec86f207fe7a83e0108cf7654f74d72b619cfd
@@ -212,19 +212,21 @@ private void AddDim(BasicExpression expr)
expr.Accept(node);
var callDim = node.Value;
this.lines.Add(this.lineNumber, callDim);
var stype = (expr.Type == BasicType.Str) ? SpecialType.System_String : SpecialType.System_Single;
var type = this.generator.TypeExpression(stype);
var arr = this.generator.IdentifierName("a");
var d1 = this.generator.IdentifierName("d1");
var leftS = this.generator.CastExpression(this.generator.TypeExpression(SpecialType.System_Int32), d1);
var sub = this.generator.AddExpression(leftS, this.generator.LiteralExpression(1));
var arrR = this.generator.ArrayCreationExpression(this.generator.TypeExpression(SpecialType.System_String), sub);
var arrR = this.generator.ArrayCreationExpression(type, sub);
SyntaxNode[] dimStatements = new SyntaxNode[] { this.generator.AssignmentStatement(arr, arrR) };
var str = this.generator.TypeExpression(SpecialType.System_String);
SyntaxNode[] parameters = new SyntaxNode[]
{
this.generator.ParameterDeclaration("a", type: this.generator.ArrayTypeExpression(str), refKind: RefKind.Out),
this.generator.ParameterDeclaration("a", type: this.generator.ArrayTypeExpression(type), refKind: RefKind.Out),
this.generator.ParameterDeclaration("d1", type: this.generator.TypeExpression(SpecialType.System_Single))
};
var dimMethod = this.generator.MethodDeclaration("DIM_sa", accessibility: Accessibility.Private, parameters: parameters, statements: dimStatements);
string name = (expr.Type == BasicType.Str) ? "DIM_sa" : "DIM_na";
var dimMethod = this.generator.MethodDeclaration(name, accessibility: Accessibility.Private, parameters: parameters, statements: dimStatements);
this.intrinsics.Add(dimMethod);
}
@@ -239,10 +241,13 @@ public ExpressionNode(SyntaxGenerator generator, Variables vars)
this.vars = vars;
}
public BasicType Type { get; private set; }
public SyntaxNode Value { get; private set; }
public void Array(BasicType type, string name, BasicExpression[] subs)
{
this.Type = type;
this.Value = this.vars.Dim(type, name, subs[0]);
}
@@ -265,23 +270,27 @@ public void Variable(BasicType type, string name)
private sealed class Variables
{
private readonly SyntaxGenerator generator;
private readonly Dictionary<string, Variable> arrs;
private readonly Dictionary<string, Variable> strArrs;
private readonly Dictionary<string, Variable> numArrs;
private readonly Dictionary<string, Variable> strs;
private readonly Dictionary<string, Variable> nums;
public Variables(SyntaxGenerator generator)
{
this.generator = generator;
this.arrs = new Dictionary<string, Variable>();
this.strArrs = new Dictionary<string, Variable>();
this.numArrs = new Dictionary<string, Variable>();
this.strs = new Dictionary<string, Variable>();
this.nums = new Dictionary<string, Variable>();
}
private IEnumerable<Variable> All => this.strs.Values.Concat(this.nums.Values);
private IEnumerable<Variable> Scalars => this.strs.Values.Concat(this.nums.Values);
private IEnumerable<Variable> Arrays => this.strArrs.Values.Concat(this.numArrs.Values);
public IEnumerable<SyntaxNode> Fields()
{
foreach (Variable v in this.arrs.Values.Concat(this.All))
foreach (Variable v in this.Arrays.Concat(this.Scalars))
{
yield return v.Field();
}
@@ -290,7 +299,7 @@ public IEnumerable<SyntaxNode> Fields()
public SyntaxNode Init()
{
List<SyntaxNode> statements = new List<SyntaxNode>();
foreach (Variable v in this.All)
foreach (Variable v in this.Scalars)
{
statements.Add(v.Init());
}
@@ -314,10 +323,10 @@ private Variable Add(BasicType type, string name, int subs)
{
if (type == BasicType.Str)
{
return this.Add((subs > 0) ? this.arrs : this.strs, type, name, subs);
return this.Add((subs > 0) ? this.strArrs : this.strs, type, name, subs);
}
return this.Add(this.nums, type, name, subs);
return this.Add((subs > 0) ? this.numArrs : this.nums, type, name, subs);
}
private Variable Add(IDictionary<string, Variable> vars, BasicType type, string name, int subs)
@@ -21,6 +21,7 @@ public void FullClass()
70 A=20
80 B1=3
90 DIM A$(5)
100 DIM B1(6)
200 GOTO 20";
const string Expected = @"using System;
using System.IO;
@@ -30,6 +31,7 @@ internal sealed class MyProg
private readonly TextReader input;
private readonly TextWriter output;
private string[] A_sa;
private float[] B1_na;
private string A_s;
private string B1_s;
private float A_n;
@@ -65,6 +67,11 @@ private void DIM_sa(out string[] a, float d1)
a = (new string[((int)(d1)) + (1)]);
}
private void DIM_na(out float[] a, float d1)
{
a = (new float[((int)(d1)) + (1)]);
}
private bool Main()
{
this.Init();
@@ -79,6 +86,7 @@ private bool Main()
A_n = (20);
B1_n = (3);
DIM_sa(out A_sa, 5);
DIM_na(out B1_na, 6);
goto L20;
return false;
}

0 comments on commit a8ec86f

Please sign in to comment.