Skip to content

Commit dfbf61c

Browse files
committed
#202 - codegen
1 parent 9c2e348 commit dfbf61c

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

src/Application/HydraScript.Application.CodeGeneration/Visitors/ExpressionInstructionProvider.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,22 @@ public AddressedInstructions Visit(BinaryExpression visitable)
151151

152152
public AddressedInstructions Visit(CastAsExpression visitable)
153153
{
154+
Func<IValue, IExecutableInstruction> asFactory = visitable.ToType switch
155+
{
156+
CastAsExpression.DestinationType.Undefined => throw new NotSupportedException(),
157+
CastAsExpression.DestinationType.String => value => new AsString(value),
158+
CastAsExpression.DestinationType.Number => value => new AsNumber(value),
159+
CastAsExpression.DestinationType.Boolean => value => new AsBool(value),
160+
_ => throw new ArgumentOutOfRangeException(nameof(visitable.ToType))
161+
};
162+
154163
if (visitable.Expression is PrimaryExpression primary)
155-
return [new AsString(_valueFactory.Create(primary.ToValueDto()))];
156-
164+
return [asFactory(_valueFactory.Create(primary.ToValueDto()))];
165+
157166
var result = visitable.Expression.Accept(This);
158167
var last = result.OfType<Simple>().Last().Left!;
159-
result.Add(new AsString(last));
160-
168+
result.Add(asFactory(last));
169+
161170
return result;
162171
}
163172

src/Application/HydraScript.Application.StaticAnalysis/Visitors/SemanticChecker.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,13 @@ public Type Visit(CastAsExpression visitable)
442442
throw new CannotDefineType(visitable.Expression.Segment);
443443

444444
var to = visitable.Cast.Accept(_typeBuilder);
445+
visitable.ToType = to switch
446+
{
447+
_ when to.Equals("string") => CastAsExpression.DestinationType.String,
448+
_ when to.Equals("number") => CastAsExpression.DestinationType.Number,
449+
_ when to.Equals("boolean") => CastAsExpression.DestinationType.Boolean,
450+
_ => CastAsExpression.DestinationType.Undefined
451+
};
445452

446453
return _explicitCastValidator.IsAllowed(from, to)
447454
? to

src/Domain/HydraScript.Domain.FrontEnd/Parser/Impl/Ast/Nodes/Expressions/CastAsExpression.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public partial class CastAsExpression : Expression
99

1010
public Expression Expression { get; }
1111
public TypeValue Cast { get; }
12+
public DestinationType ToType { get; set; }
1213

1314
public CastAsExpression(Expression expression, TypeValue cast)
1415
{
@@ -28,4 +29,12 @@ public override void InitScope(Scope? scope = null)
2829
}
2930

3031
protected override string NodeRepresentation() => $"as {Cast}";
32+
33+
public enum DestinationType
34+
{
35+
Undefined,
36+
String,
37+
Number,
38+
Boolean,
39+
}
3140
}

0 commit comments

Comments
 (0)