Skip to content

Commit

Permalink
Merge pull request #498 from b3b00/test/fstrings
Browse files Browse the repository at this point in the history
Test/fstrings
  • Loading branch information
b3b00 authored Nov 4, 2024
2 parents f8b50be + ac2acce commit f593881
Show file tree
Hide file tree
Showing 8 changed files with 258 additions and 28 deletions.
31 changes: 26 additions & 5 deletions src/samples/IndentedWhile/parser/IndentedWhileParserGeneric.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,27 @@ public WhileAST skipStmt(WhileAST expression)

#region OPERANDS

// fstrings
[Production("primary : OPEN_FSTRING[d] fstring_element* CLOSE_FSTRING[d]")]
public WhileAST fstring(List<WhileAST> elements)
{
var fstring = new FString(elements.Cast<FStringElement>().ToList(), elements.First().Position);
return fstring;
}

[Production("fstring_element : FSTRING_CONTENT")]
public WhileAST FStringContent(Token<IndentedWhileTokenGeneric> element)
{
return new FStringElement(new StringConstant(element.Value),element.Position);
}

[Production("fstring_element : OPEN_FSTRING_EXPPRESSION[d] IDENTIFIER CLOSE_FSTRING_EXPPRESSION[d]")]
public WhileAST FStringExpression(Token<IndentedWhileTokenGeneric> element)
{
return new FStringElement(new Variable(element.Value),element.Position);
}


[Production("primary: INT")]
public WhileAST PrimaryInt(Token<IndentedWhileTokenGeneric> intToken)
{
Expand All @@ -156,11 +177,11 @@ public WhileAST PrimaryBool(Token<IndentedWhileTokenGeneric> boolToken)
return new BoolConstant(bool.Parse(boolToken.StringWithoutQuotes));
}

[Production("primary: STRING")]
public WhileAST PrimaryString(Token<IndentedWhileTokenGeneric> stringToken)
{
return new StringConstant(stringToken.StringWithoutQuotes);
}
// [Production("primary: STRING")]
// public WhileAST PrimaryString(Token<IndentedWhileTokenGeneric> stringToken)
// {
// return new StringConstant(stringToken.StringWithoutQuotes);
// }

[Production("primary: IDENTIFIER")]
public WhileAST PrimaryId(Token<IndentedWhileTokenGeneric> varToken)
Expand Down
42 changes: 33 additions & 9 deletions src/samples/IndentedWhile/parser/IndentedWhileTokenGeneric.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,17 @@ public enum IndentedWhileTokenGeneric

[Lexeme(GenericToken.KeyWord, "PRINT")] [Lexeme(GenericToken.KeyWord, "print")]
PRINT = 12,

[Lexeme(GenericToken.KeyWord, "RETURN")] [Lexeme(GenericToken.KeyWord, "return")]
RETURN = 13,

#endregion

#region literals 20 -> 29

[Lexeme(GenericToken.Identifier,IdentifierType.AlphaNumericDash)] IDENTIFIER = 20,

[Lexeme(GenericToken.String)] STRING = 21,
[Mode(ModeAttribute.DefaultLexerMode, "fstringExpression")]
[Lexeme(GenericToken.Identifier, IdentifierType.AlphaNumericDash)]
IDENTIFIER = 20,

[Lexeme(GenericToken.Int)] INT = 22,

Expand Down Expand Up @@ -88,16 +88,40 @@ public enum IndentedWhileTokenGeneric

#region sugar 50 ->

// [Lexeme(GenericToken.SugarToken, "(")] LPAREN = 50,
//
// [Lexeme(GenericToken.SugarToken, ")")] RPAREN = 51,

[Lexeme(GenericToken.SugarToken, ";")] SEMICOLON = 52,


[SingleLineComment("#")]
COMMENT=1236
[SingleLineComment("#")] COMMENT = 1236,

#endregion

#region fstring 100 ->

[Push("fstringExpression")] [Mode("fstring")] [Sugar("{")]
OPEN_FSTRING_EXPPRESSION = 100,

[Pop] [Mode("fstringExpression")] [Sugar("}")]
CLOSE_FSTRING_EXPPRESSION = 101,

[Sugar("$\"")]
[Push("fstring")]
OPEN_FSTRING,

[Sugar("\"")]
[Mode("fstring")]
[Pop]
CLOSE_FSTRING,


[Mode("fstring")]
[UpTo("{","\"")]
FSTRING_CONTENT


#endregion



}
}
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
52 changes: 52 additions & 0 deletions src/samples/while/model/FString.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.Text;
using csly.whileLang.compiler;
using Sigil;
using sly.lexer;

namespace csly.whileLang.model;

public class FString : Expression
{
public LexerPosition Position { get; set; }
public Scope CompilerScope { get; set; }

public List<FStringElement> Elements { get; set; }

public FString(List<FStringElement> elements, LexerPosition position)
{
this.Elements = elements;
Position = position;
}

public string Dump(string tab)
{
StringBuilder builder = new StringBuilder();
builder.Append($"{{tab}}\"");
foreach (var element in Elements)
{
builder.Append(element.Dump(""));
}

builder.Append("\"");
return builder.ToString();
}

public string Transpile(CompilerContext context)
{
throw new NotImplementedException();
}

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

return null;
}

public WhileType Whiletype { get; set; } = WhileType.STRING;
}
63 changes: 63 additions & 0 deletions src/samples/while/model/FStringElement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System;
using csly.whileLang.compiler;
using Sigil;
using sly.lexer;

namespace csly.whileLang.model;

public class FStringElement : WhileAST
{

public LexerPosition Position { get; set; }

public Scope CompilerScope { get; set; }
public Variable VariableElement { get; set; }

public StringConstant StringElement { get; set; }

public bool IsStringElement => StringElement != null;

public bool IsVariable => VariableElement != null;

public FStringElement(Variable variableElement, LexerPosition position)
{
VariableElement = variableElement;
Position = position;
}

public FStringElement(StringConstant stringElement, LexerPosition position)
{
StringElement = stringElement;
Position = position;
}


public string Dump(string tab)
{
if (IsStringElement)
{
return tab + StringElement.Value;
}
else
{
return $"{{tab}}{{{{{VariableElement.Dump("")}}}";
}
}

public string Transpile(CompilerContext context)
{
throw new NotImplementedException();
}

public Emit<Func<int>> EmitByteCode(CompilerContext context, Emit<Func<int>> emiter)
{
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
Loading

0 comments on commit f593881

Please sign in to comment.