Permalink
Browse files

AdventureSample - support 2-D numeric array

  • Loading branch information...
bobbymcr committed Apr 22, 2018
1 parent 31e7e91 commit 4739366ebb7421aac720ccb60a12aff10f9c3a7a
@@ -11,6 +11,7 @@ namespace GWBas2CS
using GWParse.Statements;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Editing;
internal sealed class BasicVisitor : ILineVisitor, IStatementVisitor
@@ -284,7 +285,7 @@ public ExpressionNode(SyntaxGenerator generator, Variables vars, Methods methods
public void Array(BasicType type, string name, BasicExpression[] subs)
{
this.Type = type;
this.Value = this.vars.Dim(this.methods, type, name, subs[0]);
this.Value = this.vars.Dim(this.methods, type, name, subs);
}
public void Literal(BasicType type, object o)
@@ -359,11 +360,17 @@ public SyntaxNode Init()
return this.generator.MethodDeclaration("Init", accessibility: Accessibility.Private, statements: statements);
}
public SyntaxNode Dim(Methods methods, BasicType type, string name, BasicExpression sub)
public SyntaxNode Dim(Methods methods, BasicType type, string name, BasicExpression[] subs)
{
ExpressionNode node = new ExpressionNode(this.generator, this, methods);
sub.Accept(node);
return this.Add(type, name, 1).Dim(methods, node.Value);
SyntaxNode[] subNodes = new SyntaxNode[subs.Length];
for (int i = 0; i < subs.Length; ++i)
{
subs[i].Accept(node);
subNodes[i] = node.Value;
}
return this.Add(type, name, subNodes.Length).Dim(methods, subNodes);
}
public SyntaxNode Add(BasicType type, string name)
@@ -413,31 +420,30 @@ public Variable(SyntaxGenerator generator, BasicType type, string name, int subs
this.subs = subs;
}
private SyntaxNode ElementType
private TypeSyntax ElementType
{
get
{
SpecialType ty = SpecialType.System_Single;
SyntaxKind kind = SyntaxKind.FloatKeyword;
if (this.type == BasicType.Str)
{
ty = SpecialType.System_String;
kind = SyntaxKind.StringKeyword;
}
return this.generator.TypeExpression(ty);
return SyntaxFactory.PredefinedType(SyntaxFactory.Token(kind));
}
}
private SyntaxNode Type
{
get
{
SyntaxNode tx = this.ElementType;
if (this.subs > 0)
{
tx = this.generator.ArrayTypeExpression(tx);
return this.ArrayType();
}
return tx;
return this.ElementType;
}
}
@@ -491,14 +497,28 @@ public SyntaxNode Init()
return this.generator.AssignmentStatement(this.Ref(), this.Default);
}
public SyntaxNode Dim(Methods methods, SyntaxNode sub)
public SyntaxNode Dim(Methods methods, SyntaxNode[] sub)
{
string name = "DIM" + this.Suffix;
string name = "DIM" + sub.Length + this.Suffix;
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 d = this.generator.AddExpression(leftS, this.generator.LiteralExpression(1));
var arrR = this.generator.ArrayCreationExpression(this.ElementType, d);
SyntaxNode[] subNodes = new SyntaxNode[sub.Length];
List<SyntaxNode> parameters = new List<SyntaxNode>();
parameters.Add(this.generator.ParameterDeclaration("a", type: this.ArrayType(), refKind: RefKind.Out));
for (int i = 0; i < sub.Length; ++i)
{
string n = "d" + (i + 1);
var p = this.generator.ParameterDeclaration(n, type: this.generator.TypeExpression(SpecialType.System_Single));
parameters.Add(p);
var dn = this.generator.IdentifierName(n);
var leftS = this.generator.CastExpression(this.generator.TypeExpression(SpecialType.System_Int32), dn);
subNodes[i] = this.generator.AddExpression(leftS, this.generator.LiteralExpression(1));
}
var arrR = SyntaxFactory.ArrayCreationExpression(this.ArrayType(subNodes));
List<SyntaxNode> dimStatements = new List<SyntaxNode>();
dimStatements.Add(this.generator.AssignmentStatement(arr, arrR));
if (this.type == BasicType.Str)
@@ -508,17 +528,42 @@ public SyntaxNode Dim(Methods methods, SyntaxNode sub)
dimStatements.Add(callFill);
}
SyntaxNode[] parameters = new SyntaxNode[]
{
this.generator.ParameterDeclaration("a", type: this.Type, refKind: RefKind.Out),
this.generator.ParameterDeclaration("d1", type: this.generator.TypeExpression(SpecialType.System_Single))
};
var dimMethod = this.generator.MethodDeclaration(name, accessibility: Accessibility.Private, parameters: parameters, statements: dimStatements);
methods.Add(name, dimMethod);
var method = this.generator.IdentifierName(name);
var arg1 = this.generator.Argument(RefKind.Out, this.Ref());
return this.generator.InvocationExpression(method, arg1, sub);
List<SyntaxNode> args = new List<SyntaxNode>();
args.Add(this.generator.Argument(RefKind.Out, this.Ref()));
args.AddRange(sub);
return this.generator.InvocationExpression(method, args);
}
private ArrayTypeSyntax ArrayType(params SyntaxNode[] nodes)
{
List<SyntaxNodeOrToken> sizes = new List<SyntaxNodeOrToken>();
var omit = SyntaxFactory.OmittedArraySizeExpression();
var comma = SyntaxFactory.Token(SyntaxKind.CommaToken);
for (int i = 0; i < this.subs; ++i)
{
if (i != 0)
{
sizes.Add(comma);
}
if (nodes.Length == 0)
{
sizes.Add(omit);
}
else
{
sizes.Add(nodes[i]);
}
}
var rank = SyntaxFactory.ArrayRankSpecifier(SyntaxFactory.SeparatedList<ExpressionSyntax>(sizes));
return SyntaxFactory.ArrayType(this.ElementType, SyntaxFactory.SingletonList(rank));
}
}
}
@@ -17,7 +17,25 @@ public void OneDimensionComplexExpression()
private bool Main()
{
this.Init();
DIM_na(out A_na, (B_n) + (2));
DIM1_na(out A_na, (B_n) + (2));
return false;
}
*";
string actual = Test.Translate("MyProg", Input);
actual.Should().Match(Expected);
}
[Fact]
public void TwoDimensionsComplexExpression()
{
const string Input = @"10 DIM A(B+2, C)";
const string Expected = @"*
private bool Main()
{
this.Init();
DIM2_na(out A_na, (B_n) + (2), C_n);
return false;
}
*";
@@ -24,7 +24,8 @@ public void FullClass()
100 DIM B1$(6)
110 DIM A(7)
120 DIM B1(8)
130 CLS
130 DIM B2(9,10)
140 CLS
1000 GOTO 20";
const string Expected = @"using System;
using System.IO;
@@ -37,6 +38,7 @@ internal sealed class MyProg
private string[] B1_sa;
private float[] A_na;
private float[] B1_na;
private float[, ] B2_na;
private string A_s;
private string B1_s;
private float A_n;
@@ -67,17 +69,22 @@ private void PRINT(string expression)
this.output.WriteLine(expression);
}
private void DIM_sa(out string[] a, float d1)
private void DIM1_sa(out string[] a, float d1)
{
a = (new string[((int)(d1)) + (1)]);
Array.Fill(a, """");
}
private void DIM_na(out float[] a, float d1)
private void DIM1_na(out float[] a, float d1)
{
a = (new float[((int)(d1)) + (1)]);
}
private void DIM2_na(out float[, ] a, float d1, float d2)
{
a = (new float[((int)(d1)) + (1), ((int)(d2)) + (1)]);
}
private void CLS()
{
this.output.Write('\f');
@@ -97,10 +104,11 @@ private bool Main()
A_n = (2);
A_n = (20);
B1_n = (3);
DIM_sa(out A_sa, 5);
DIM_sa(out B1_sa, 6);
DIM_na(out A_na, 7);
DIM_na(out B1_na, 8);
DIM1_sa(out A_sa, 5);
DIM1_sa(out B1_sa, 6);
DIM1_na(out A_na, 7);
DIM1_na(out B1_na, 8);
DIM2_na(out B2_na, 9, 10);
CLS();
goto L20;
return false;

0 comments on commit 4739366

Please sign in to comment.