Skip to content

Commit 3e5eed2

Browse files
committed
indented while : ternary operator
1 parent cbdcbf4 commit 3e5eed2

20 files changed

+216
-26
lines changed

src/samples/IndentedWhile/IndentedWhileCompiler.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Text;
34
using csly.indentedWhileLang.parser;
45
using csly.whileLang.compiler;
@@ -79,9 +80,10 @@ public string TranspileToCSharp(string whileCode)
7980
}
8081

8182

82-
public Func<int> CompileToFunction(string whileCode, bool isQuiet = false)
83+
public Func<int> CompileToFunction(string whileCode, bool isQuiet = false)
8384
{
8485
Func<int> function = null;
86+
CompilerContext context = null;
8587

8688
try
8789
{
@@ -92,10 +94,15 @@ public Func<int> CompileToFunction(string whileCode, bool isQuiet = false)
9294

9395
var checker = new SemanticChecker();
9496

95-
var context = checker.SemanticCheck(ast,isQuiet);
96-
97+
context = checker.SemanticCheck(ast,isQuiet);
98+
var ternaries = new List<TernaryExpression>();
99+
ast.AppendTernaries(ternaries);
100+
for (int i = 0; i < ternaries.Count; i++)
101+
{
102+
ternaries[i].Number = i ;
103+
}
104+
97105
var emiter = Emit<Func<int>>.NewDynamicMethod("Method" + Guid.NewGuid());
98-
99106
emiter = ast.EmitByteCode(context, emiter);
100107
//emiter.LoadConstant(42);
101108
//emiter.Return();
@@ -107,7 +114,6 @@ public Func<int> CompileToFunction(string whileCode, bool isQuiet = false)
107114
function = null;
108115
}
109116

110-
111117
return function;
112118
}
113119
}

src/samples/while/compiler/ExpressionTyper.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public WhileType TypeExpression(Expression expr, CompilerContext context)
2020
if (expr is BinaryOperation binary) return TypeExpression(binary, context);
2121
if (expr is Neg neg) return TypeExpression(neg, context);
2222
if (expr is Not not) return TypeExpression(not, context);
23+
if (expr is TernaryExpression ternary) return TypeExpression(ternary, context);
2324
if (expr is Variable variable)
2425
{
2526
var varType = context.GetVariableType(variable.Name);
@@ -79,5 +80,16 @@ public WhileType TypeExpression(Not not, CompilerContext context)
7980
throw new SignatureException($"invalid operation type({positiveVal}) : {not.Dump("")}");
8081
return WhileType.BOOL;
8182
}
83+
84+
public WhileType TypeExpression(TernaryExpression ternary, CompilerContext context)
85+
{
86+
var conditionType = TypeExpression(ternary.Condition, context);
87+
var trueType = TypeExpression(ternary.TrueExpression, context);
88+
var falseType = TypeExpression(ternary.FalseExpression, context);
89+
ternary.CompilerScope = context.CurrentScope;
90+
if (conditionType != WhileType.BOOL)
91+
throw new SignatureException($"invalid operation type({conditionType}) : {ternary.Condition.Dump("")}");
92+
return trueType;
93+
}
8294
}
8395
}

src/samples/while/model/AssignStatement.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Text;
34
using csly.whileLang.compiler;
45
using sly.lexer;
@@ -44,6 +45,13 @@ public string Transpile(CompilerContext context)
4445

4546
public Emit<Func<int>> EmitByteCode(CompilerContext context, Emit<Func<int>> emiter)
4647
{
48+
var ternaries = new List<TernaryExpression>();
49+
Value.AppendTernaries(ternaries);
50+
foreach (var ternary in ternaries)
51+
{
52+
ternary.EmitByteCodeForVariable(context, emiter);
53+
}
54+
4755
Local local = null;
4856
if (IsVariableCreation)
4957
local = emiter.DeclareLocal(TypeConverter.WhileToType(CompilerScope.GetVariableType(VariableName)),
@@ -54,5 +62,10 @@ public Emit<Func<int>> EmitByteCode(CompilerContext context, Emit<Func<int>> emi
5462
emiter.StoreLocal(local);
5563
return emiter;
5664
}
65+
66+
public void AppendTernaries(List<TernaryExpression> ternaries)
67+
{
68+
Value.AppendTernaries(ternaries);
69+
}
5770
}
5871
}

src/samples/while/model/BinaryOperation.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ public BinaryOperation(Expression left, BinaryOperator oper, Expression right)
4242

4343
public LexerPosition Position { get; set; }
4444
public WhileType Whiletype { get; set; }
45+
public void AppendTernaries(List<TernaryExpression> ternaries)
46+
{
47+
Left.AppendTernaries(ternaries);
48+
Right.AppendTernaries(ternaries);
49+
}
4550

4651
[ExcludeFromCodeCoverage]
4752
public string Dump(string tab)

src/samples/while/model/BoolConstant.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using csly.whileLang.compiler;
34
using sly.lexer;
45
using Sigil;
@@ -25,6 +26,10 @@ public WhileType Whiletype
2526
set { }
2627
}
2728

29+
public void AppendTernaries(List<TernaryExpression> ternaries)
30+
{
31+
}
32+
2833
public string Dump(string tab)
2934
{
3035
return $"{tab}(BOOL {Value})";

src/samples/while/model/Expression.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
using csly.whileLang.compiler;
1+
using System.Collections.Generic;
2+
using csly.whileLang.compiler;
23

34
namespace csly.whileLang.model
45
{
56
public interface Expression : WhileAST
67
{
78
WhileType Whiletype { get; set; }
9+
810
}
911
}

src/samples/while/model/IfStatement.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Text;
34
using csly.whileLang.compiler;
45
using sly.lexer;
@@ -71,6 +72,14 @@ public Emit<Func<int>> EmitByteCode(CompilerContext context, Emit<Func<int>> emi
7172
var thenLabel = emiter.DefineLabel();
7273
var elseLabel = emiter.DefineLabel();
7374
var endLabel = emiter.DefineLabel();
75+
76+
var ternaries = new List<TernaryExpression>();
77+
Condition.AppendTernaries(ternaries);
78+
foreach (var ternary in ternaries)
79+
{
80+
ternary.EmitByteCodeForVariable(context, emiter);
81+
}
82+
7483
Condition.EmitByteCode(context, emiter);
7584
emiter.BranchIfTrue(thenLabel);
7685
emiter.Branch(elseLabel);
@@ -83,5 +92,12 @@ public Emit<Func<int>> EmitByteCode(CompilerContext context, Emit<Func<int>> emi
8392
emiter.MarkLabel(endLabel);
8493
return emiter;
8594
}
95+
96+
public void AppendTernaries(List<TernaryExpression> ternaries)
97+
{
98+
Condition.AppendTernaries(ternaries);
99+
ThenStmt.AppendTernaries(ternaries);
100+
ElseStmt.AppendTernaries(ternaries);
101+
}
86102
}
87103
}

src/samples/while/model/IntegerConstant.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using csly.whileLang.compiler;
34
using sly.lexer;
45
using Sigil;
@@ -25,6 +26,10 @@ public WhileType Whiletype
2526
set { }
2627
}
2728

29+
public void AppendTernaries(List<TernaryExpression> ternaries)
30+
{
31+
}
32+
2833

2934
public string Dump(string tab)
3035
{

src/samples/while/model/Neg.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Text;
34
using csly.whileLang.compiler;
45
using sly.lexer;
@@ -25,6 +26,11 @@ public WhileType Whiletype
2526
set { }
2627
}
2728

29+
public void AppendTernaries(List<TernaryExpression> ternaries)
30+
{
31+
Value.AppendTernaries(ternaries);
32+
}
33+
2834
public string Dump(string tab)
2935
{
3036
var dmp = new StringBuilder();

src/samples/while/model/Not.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Text;
34
using csly.whileLang.compiler;
45
using sly.lexer;
@@ -25,6 +26,11 @@ public WhileType Whiletype
2526
set { }
2627
}
2728

29+
public void AppendTernaries(List<TernaryExpression> ternaries)
30+
{
31+
Value.AppendTernaries(ternaries);
32+
}
33+
2834

2935
public string Dump(string tab)
3036
{

0 commit comments

Comments
 (0)