Permalink
Browse files

AdventureSample - support LEFT$

  • Loading branch information...
bobbymcr committed May 1, 2018
1 parent 1e03904 commit cc3681926dcd6e0988ebf1cf8488f6deca2f55c6
@@ -544,6 +544,7 @@ private SyntaxNode Binary(string name, SyntaxNode x, SyntaxNode y)
case "Sub": return this.generator.SubtractExpression(x, y);
case "Mult": return this.generator.MultiplyExpression(x, y);
case "Div": return this.generator.DivideExpression(x, y);
case "Left": return this.Left(x, y);
default: throw new NotSupportedException("Operator:" + name);
}
}
@@ -570,6 +571,37 @@ private SyntaxNode Len(SyntaxNode x)
return this.generator.MemberAccessExpression(x, "Length");
}
private SyntaxNode Left(SyntaxNode x, SyntaxNode n)
{
string name = "LEFT_s";
var callLeft = this.generator.InvocationExpression(this.generator.IdentifierName(name), x, this.CastInt(n));
var intT = this.generator.TypeExpression(SpecialType.System_Int32);
var nv = this.generator.IdentifierName("n");
var xv = this.generator.IdentifierName("x");
var retX = this.generator.ReturnStatement(xv);
var len = this.generator.MemberAccessExpression(xv, "Length");
var gtL = this.generator.GreaterThanExpression(nv, len);
var ifN = this.generator.IfStatement(gtL, new SyntaxNode[] { retX });
var zero = this.generator.LiteralExpression(0);
var callSubstr = this.generator.MemberAccessExpression(xv, "Substring");
var retSubstr = this.generator.ReturnStatement(this.generator.InvocationExpression(callSubstr, zero, nv));
SyntaxNode[] leftStatements = new SyntaxNode[]
{
ifN,
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),
};
var midMethod = this.generator.MethodDeclaration(name, parameters: parameters, returnType: strT, accessibility: Accessibility.Private, statements: leftStatements);
this.methods.Add(name, midMethod);
return callLeft;
}
private SyntaxNode Mid(SyntaxNode x, SyntaxNode n, SyntaxNode m)
{
string name = "MID_s";
@@ -38,6 +38,7 @@ 140 CLS
240 PRINT I
250 NEXT I
260 A$=MID$(A$,A,1)
270 A$=LEFT$(A$,A)
1000 GOTO 20
2000 CLS
2001 DATA 1
@@ -164,6 +165,16 @@ private string MID_s(string x, int n, int m)
return x.Substring((n) - (1), m);
}
private string LEFT_s(string x, int n)
{
if ((n) > (x.Length))
{
return x;
}
return x.Substring(0, n);
}
private int Sub_2000()
{
CLS();
@@ -250,6 +261,7 @@ private int Main()
}
A_s = (MID_s(A_s, (int)(A_n), (int)(1)));
A_s = (LEFT_s(A_s, (int)(A_n)));
goto L20;
return 2;
}

0 comments on commit cc36819

Please sign in to comment.