Permalink
Browse files

AdventureSample - support MID$

  • Loading branch information...
bobbymcr committed Apr 26, 2018
1 parent 2601a7f commit 1e03904b8c9a16f51f76aef86f168e821f1422e6
@@ -495,6 +495,14 @@ public void Operator(string name, BasicExpression[] operands)
SyntaxNode y = this.Value;
this.Value = this.Binary(name, x, y);
}
else if (operands.Length == 3)
{
operands[1].Accept(this);
SyntaxNode y = this.Value;
operands[2].Accept(this);
SyntaxNode z = this.Value;
this.Value = this.Ternary(name, x, y, z);
}
else
{
throw new NotSupportedException("Operator:" + name);
@@ -506,7 +514,7 @@ public void Variable(BasicType type, string name)
this.Value = this.vars.Add(type, name);
}
private SyntaxNode Cast(SyntaxNode node)
private SyntaxNode CastInt(SyntaxNode node)
{
return this.generator.CastExpression(this.generator.TypeExpression(SpecialType.System_Int32), node);
}
@@ -530,8 +538,8 @@ private SyntaxNode Binary(string name, SyntaxNode x, SyntaxNode y)
case "Lt": return this.Cond(this.generator.LessThanExpression, x, y);
case "Ge": return this.Cond(this.generator.GreaterThanOrEqualExpression, x, y);
case "Gt": return this.Cond(this.generator.GreaterThanExpression, x, y);
case "Or": return this.generator.BitwiseOrExpression(this.Cast(x), this.Cast(y));
case "And": return this.generator.BitwiseAndExpression(this.Cast(x), this.Cast(y));
case "Or": return this.generator.BitwiseOrExpression(this.CastInt(x), this.CastInt(y));
case "And": return this.generator.BitwiseAndExpression(this.CastInt(x), this.CastInt(y));
case "Add": return this.generator.AddExpression(x, y);
case "Sub": return this.generator.SubtractExpression(x, y);
case "Mult": return this.generator.MultiplyExpression(x, y);
@@ -540,6 +548,15 @@ private SyntaxNode Binary(string name, SyntaxNode x, SyntaxNode y)
}
}
private SyntaxNode Ternary(string name, SyntaxNode x, SyntaxNode y, SyntaxNode z)
{
switch (name)
{
case "Mid": return this.Mid(x, y, z);
default: throw new NotSupportedException("Operator:" + name);
}
}
private SyntaxNode Cond(Func<SyntaxNode, SyntaxNode, SyntaxNode> cond, SyntaxNode x, SyntaxNode y)
{
var call = this.generator.InvocationExpression(this.generator.MemberAccessExpression(x, "CompareTo"), y);
@@ -552,6 +569,47 @@ private SyntaxNode Len(SyntaxNode x)
{
return this.generator.MemberAccessExpression(x, "Length");
}
private SyntaxNode Mid(SyntaxNode x, SyntaxNode n, SyntaxNode m)
{
string name = "MID_s";
var callMid = this.generator.InvocationExpression(this.generator.IdentifierName(name), x, this.CastInt(n), this.CastInt(m));
var intT = this.generator.TypeExpression(SpecialType.System_Int32);
var retE = this.generator.ReturnStatement(this.generator.LiteralExpression(string.Empty));
var nv = this.generator.IdentifierName("n");
var xv = this.generator.IdentifierName("x");
var len = this.generator.MemberAccessExpression(xv, "Length");
var gtL = this.generator.GreaterThanExpression(nv, len);
var ifN = this.generator.IfStatement(gtL, new SyntaxNode[] { retE });
var sub = this.generator.SubtractExpression(len, nv);
var one = this.generator.LiteralExpression(1);
var setL = this.generator.LocalDeclarationStatement(intT, "l", this.generator.AddExpression(sub, one));
var mv = this.generator.IdentifierName("m");
var lv = this.generator.IdentifierName("l");
var gt = this.generator.GreaterThanExpression(mv, lv);
var ifM = this.generator.IfStatement(gt, new SyntaxNode[] { this.generator.AssignmentStatement(mv, lv) });
var n1 = this.generator.SubtractExpression(nv, one);
var callSubstr = this.generator.MemberAccessExpression(xv, "Substring");
var retSubstr = this.generator.ReturnStatement(this.generator.InvocationExpression(callSubstr, n1, mv));
SyntaxNode[] midStatements = new SyntaxNode[]
{
ifN,
setL,
ifM,
retSubstr
};
var strT = this.generator.TypeExpression(SpecialType.System_String);
SyntaxNode[] parameters = new SyntaxNode[]
{
this.generator.ParameterDeclaration("x", type: strT),
this.generator.ParameterDeclaration("n", type: intT),
this.generator.ParameterDeclaration("m", type: intT)
};
var midMethod = this.generator.MethodDeclaration(name, parameters: parameters, returnType: strT, accessibility: Accessibility.Private, statements: midStatements);
this.methods.Add(name, midMethod);
return callMid;
}
}
private sealed class DataValues
@@ -37,6 +37,7 @@ 140 CLS
230 FOR I=1 TO 10
240 PRINT I
250 NEXT I
260 A$=MID$(A$,A,1)
1000 GOTO 20
2000 CLS
2001 DATA 1
@@ -147,6 +148,22 @@ private string READ_s()
return (string)(DATA.Dequeue());
}
private string MID_s(string x, int n, int m)
{
if ((n) > (x.Length))
{
return """";
}
int l = ((x.Length) - (n)) + (1);
if ((m) > (l))
{
m = (l);
}
return x.Substring((n) - (1), m);
}
private int Sub_2000()
{
CLS();
@@ -232,6 +249,7 @@ private int Main()
I_n = ((I_n) + (1));
}
A_s = (MID_s(A_s, (int)(A_n), (int)(1)));
goto L20;
return 2;
}

0 comments on commit 1e03904

Please sign in to comment.