Skip to content

Commit ac2acce

Browse files
committed
fstrings / interpolated strings : fixing issue with indents, uptos and modes
1 parent cb76665 commit ac2acce

File tree

6 files changed

+56
-13
lines changed

6 files changed

+56
-13
lines changed

src/samples/while/compiler/ExpressionTyper.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ public WhileType TypeExpression(Expression expr, CompilerContext context)
2929
return varType;
3030
}
3131

32+
if (expr is FString fstring)
33+
{
34+
return WhileType.STRING;
35+
}
36+
3237
throw new SignatureException($"unknow expression type ({expr.GetType().Name})");
3338
}
3439

src/samples/while/model/FString.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,12 @@ public string Transpile(CompilerContext context)
4040

4141
public Emit<Func<int>> EmitByteCode(CompilerContext context, Emit<Func<int>> emiter)
4242
{
43-
throw new NotImplementedException();
43+
if (Elements.Count == 1)
44+
{
45+
return Elements[0].EmitByteCode(context, emiter);
46+
}
47+
48+
return null;
4449
}
4550

4651
public WhileType Whiletype { get; set; } = WhileType.STRING;

src/samples/while/model/FStringElement.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ public string Transpile(CompilerContext context)
5151

5252
public Emit<Func<int>> EmitByteCode(CompilerContext context, Emit<Func<int>> emiter)
5353
{
54-
throw new NotImplementedException();
54+
if (IsStringElement)
55+
{
56+
return StringElement.EmitByteCode(context, emiter);
57+
}
58+
else
59+
{
60+
return VariableElement.EmitByteCode(context, emiter);
61+
}
5562
}
5663
}

src/sly/lexer/fsm/FSMLexer.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -314,36 +314,44 @@ private FSMMatch<N> ConsumeIndents3(ReadOnlyMemory<char> source, LexerPosition l
314314
{
315315
case LexerIndentationType.Indent:
316316
{
317+
var position = lexerPosition.Clone();
318+
position.IsPush = false;
319+
position.IsPop = false;
320+
position.Mode = null;
317321
var indent = FSMMatch<N>.Indent(lexerPosition.Indentation.CurrentLevel);
318322
indent.Result = new Token<N>
319323
{
320324
IsIndent = true,
321325
IsUnIndent = false,
322326
IsNoIndent = false,
323-
Position = lexerPosition.Clone()
327+
Position = position
324328
};
325329
indent.IsNoIndent = false;
326330
indent.IsIndent = true;
327331
indent.IsUnIndent = false;
328-
indent.NewPosition = lexerPosition.Clone();
332+
indent.NewPosition = position;
329333
indent.NewPosition.Index += currentShift.Length;
330334
indent.NewPosition.Column += currentShift.Length;
331335
return indent;
332336
}
333337
case LexerIndentationType.UIndent:
334338
{
335339
var uIndent = FSMMatch<N>.UIndent(lexerPosition.Indentation.CurrentLevel);
340+
var position = lexerPosition.Clone();
341+
position.IsPush = false;
342+
position.IsPop = false;
343+
position.Mode = null;
336344
uIndent.Result = new Token<N>
337345
{
338346
IsIndent = false,
339347
IsUnIndent = true,
340348
IsNoIndent = false,
341-
Position = lexerPosition.Clone()
349+
Position = position
342350
};
343351
uIndent.IsNoIndent = false;
344352
uIndent.IsIndent = false;
345353
uIndent.IsUnIndent = true;
346-
uIndent.NewPosition = lexerPosition.Clone();
354+
uIndent.NewPosition = position;
347355
uIndent.NewPosition.Index += currentShift.Length;
348356
uIndent.NewPosition.Column += currentShift.Length;
349357
return uIndent;

src/sly/lexer/fsm/FSMMatch.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ public static FSMMatch<N> Indent(int level)
6666
IsIndent = true,
6767
IsSuccess = true,
6868
IndentationLevel = level,
69+
IsPop = false,
70+
IsPush = false,
6971
Result = new Token<N> {IsIndent = true, IsEOS = false}
7072
};
7173
}
@@ -76,6 +78,8 @@ public static FSMMatch<N> UIndent(int level, int count = 1)
7678
{
7779
IsUnIndent = true,
7880
IsSuccess = true,
81+
IsPop = false,
82+
IsPush = false,
7983
IndentationLevel = level,
8084
Result = new Token<N> {IsUnIndent = true, IsEOS = false},
8185
UnIndentCount = count

tests/ParserTests/samples/IndentedWhileTests.cs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ public void TestFactorialProgramExecAsIL()
129129
i:=1
130130
while i < 11 do
131131
r := r * i
132-
print ""r="".r
133-
print ""i="".i
132+
print $""r="".r
133+
print $""i="".i
134134
i := i + 1
135135
return r";
136136
var compiler = new IndentedWhileCompiler();
@@ -170,17 +170,31 @@ public void TestIfThenElse()
170170
Check.That(thenBlock.Get(0)).IsInstanceOf<AssignStatement>();
171171
var thenAssign = thenBlock.Get(0) as AssignStatement;
172172
Check.That(thenAssign.VariableName).IsEqualTo("a");
173-
Check.That(thenAssign.Value).IsInstanceOf<StringConstant>();
174-
Check.That((thenAssign.Value as StringConstant).Value).IsEqualTo("hello");
173+
Check.That(thenAssign.Value).IsInstanceOf<FString>();
174+
var fstring = thenAssign.Value as FString;
175+
Check.That(fstring).IsNotNull();
176+
Check.That(fstring.Elements).CountIs(1);
177+
Check.That(fstring.Elements[0]).IsInstanceOf<FStringElement>();
178+
var element = fstring.Elements[0] as FStringElement;
179+
Check.That(element).IsNotNull();
180+
Check.That(element.IsStringElement).IsTrue();
181+
Check.That(element.StringElement.Value).IsEqualTo("hello");
175182

176183
Check.That(si.ElseStmt).IsInstanceOf<SequenceStatement>();
177184
var elseBlock = si.ElseStmt as SequenceStatement;
178185
Check.That(elseBlock).CountIs(1);
179186
Check.That(elseBlock.Get(0)).IsInstanceOf<AssignStatement>();
180187
var elseAssign = elseBlock.Get(0) as AssignStatement;
181188
Check.That(elseAssign.VariableName).IsEqualTo("b");
182-
Check.That(elseAssign.Value).IsInstanceOf<StringConstant>();
183-
Check.That((elseAssign.Value as StringConstant).Value).IsEqualTo("world");
189+
Check.That(elseAssign.Value).IsInstanceOf<FString>();
190+
fstring = elseAssign.Value as FString;
191+
Check.That(fstring).IsNotNull();
192+
Check.That(fstring.Elements).CountIs(1);
193+
Check.That(fstring.Elements[0]).IsInstanceOf<FStringElement>();
194+
element = fstring.Elements[0] as FStringElement;
195+
Check.That(element).IsNotNull();
196+
Check.That(element.IsStringElement).IsTrue();
197+
Check.That(element.StringElement.Value).IsEqualTo("world");
184198
}
185199

186200
[Fact]
@@ -196,7 +210,7 @@ public void TestNestedIfThenElse()
196210
a := 2
197211
else
198212
a := 3
199-
b := ""world""
213+
b := $""world""
200214
return a
201215
";
202216
var compiler = new IndentedWhileCompiler();

0 commit comments

Comments
 (0)