Skip to content

Commit

Permalink
fstrings / interpolated strings : fixing issue with indents, uptos an…
Browse files Browse the repository at this point in the history
…d modes
  • Loading branch information
b3b00 committed Nov 4, 2024
1 parent cb76665 commit ac2acce
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 13 deletions.
5 changes: 5 additions & 0 deletions src/samples/while/compiler/ExpressionTyper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public WhileType TypeExpression(Expression expr, CompilerContext context)
return varType;
}

if (expr is FString fstring)
{
return WhileType.STRING;
}

throw new SignatureException($"unknow expression type ({expr.GetType().Name})");
}

Expand Down
7 changes: 6 additions & 1 deletion src/samples/while/model/FString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ public string Transpile(CompilerContext context)

public Emit<Func<int>> EmitByteCode(CompilerContext context, Emit<Func<int>> emiter)
{
throw new NotImplementedException();
if (Elements.Count == 1)
{
return Elements[0].EmitByteCode(context, emiter);
}

return null;
}

public WhileType Whiletype { get; set; } = WhileType.STRING;
Expand Down
9 changes: 8 additions & 1 deletion src/samples/while/model/FStringElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ public string Transpile(CompilerContext context)

public Emit<Func<int>> EmitByteCode(CompilerContext context, Emit<Func<int>> emiter)
{
throw new NotImplementedException();
if (IsStringElement)
{
return StringElement.EmitByteCode(context, emiter);
}
else
{
return VariableElement.EmitByteCode(context, emiter);
}
}
}
16 changes: 12 additions & 4 deletions src/sly/lexer/fsm/FSMLexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -314,36 +314,44 @@ private FSMMatch<N> ConsumeIndents3(ReadOnlyMemory<char> source, LexerPosition l
{
case LexerIndentationType.Indent:
{
var position = lexerPosition.Clone();
position.IsPush = false;
position.IsPop = false;
position.Mode = null;
var indent = FSMMatch<N>.Indent(lexerPosition.Indentation.CurrentLevel);
indent.Result = new Token<N>
{
IsIndent = true,
IsUnIndent = false,
IsNoIndent = false,
Position = lexerPosition.Clone()
Position = position
};
indent.IsNoIndent = false;
indent.IsIndent = true;
indent.IsUnIndent = false;
indent.NewPosition = lexerPosition.Clone();
indent.NewPosition = position;
indent.NewPosition.Index += currentShift.Length;
indent.NewPosition.Column += currentShift.Length;
return indent;
}
case LexerIndentationType.UIndent:
{
var uIndent = FSMMatch<N>.UIndent(lexerPosition.Indentation.CurrentLevel);
var position = lexerPosition.Clone();
position.IsPush = false;
position.IsPop = false;
position.Mode = null;
uIndent.Result = new Token<N>
{
IsIndent = false,
IsUnIndent = true,
IsNoIndent = false,
Position = lexerPosition.Clone()
Position = position
};
uIndent.IsNoIndent = false;
uIndent.IsIndent = false;
uIndent.IsUnIndent = true;
uIndent.NewPosition = lexerPosition.Clone();
uIndent.NewPosition = position;
uIndent.NewPosition.Index += currentShift.Length;
uIndent.NewPosition.Column += currentShift.Length;
return uIndent;
Expand Down
4 changes: 4 additions & 0 deletions src/sly/lexer/fsm/FSMMatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public static FSMMatch<N> Indent(int level)
IsIndent = true,
IsSuccess = true,
IndentationLevel = level,
IsPop = false,
IsPush = false,
Result = new Token<N> {IsIndent = true, IsEOS = false}
};
}
Expand All @@ -76,6 +78,8 @@ public static FSMMatch<N> UIndent(int level, int count = 1)
{
IsUnIndent = true,
IsSuccess = true,
IsPop = false,
IsPush = false,
IndentationLevel = level,
Result = new Token<N> {IsUnIndent = true, IsEOS = false},
UnIndentCount = count
Expand Down
28 changes: 21 additions & 7 deletions tests/ParserTests/samples/IndentedWhileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ public void TestFactorialProgramExecAsIL()
i:=1
while i < 11 do
r := r * i
print ""r="".r
print ""i="".i
print $""r="".r
print $""i="".i
i := i + 1
return r";
var compiler = new IndentedWhileCompiler();
Expand Down Expand Up @@ -170,17 +170,31 @@ public void TestIfThenElse()
Check.That(thenBlock.Get(0)).IsInstanceOf<AssignStatement>();
var thenAssign = thenBlock.Get(0) as AssignStatement;
Check.That(thenAssign.VariableName).IsEqualTo("a");
Check.That(thenAssign.Value).IsInstanceOf<StringConstant>();
Check.That((thenAssign.Value as StringConstant).Value).IsEqualTo("hello");
Check.That(thenAssign.Value).IsInstanceOf<FString>();
var fstring = thenAssign.Value as FString;
Check.That(fstring).IsNotNull();
Check.That(fstring.Elements).CountIs(1);
Check.That(fstring.Elements[0]).IsInstanceOf<FStringElement>();
var element = fstring.Elements[0] as FStringElement;
Check.That(element).IsNotNull();
Check.That(element.IsStringElement).IsTrue();
Check.That(element.StringElement.Value).IsEqualTo("hello");

Check.That(si.ElseStmt).IsInstanceOf<SequenceStatement>();
var elseBlock = si.ElseStmt as SequenceStatement;
Check.That(elseBlock).CountIs(1);
Check.That(elseBlock.Get(0)).IsInstanceOf<AssignStatement>();
var elseAssign = elseBlock.Get(0) as AssignStatement;
Check.That(elseAssign.VariableName).IsEqualTo("b");
Check.That(elseAssign.Value).IsInstanceOf<StringConstant>();
Check.That((elseAssign.Value as StringConstant).Value).IsEqualTo("world");
Check.That(elseAssign.Value).IsInstanceOf<FString>();
fstring = elseAssign.Value as FString;
Check.That(fstring).IsNotNull();
Check.That(fstring.Elements).CountIs(1);
Check.That(fstring.Elements[0]).IsInstanceOf<FStringElement>();
element = fstring.Elements[0] as FStringElement;
Check.That(element).IsNotNull();
Check.That(element.IsStringElement).IsTrue();
Check.That(element.StringElement.Value).IsEqualTo("world");
}

[Fact]
Expand All @@ -196,7 +210,7 @@ public void TestNestedIfThenElse()
a := 2
else
a := 3
b := ""world""
b := $""world""
return a
";
var compiler = new IndentedWhileCompiler();
Expand Down

0 comments on commit ac2acce

Please sign in to comment.