Skip to content

Commit d84a262

Browse files
committed
#201 - codegen + static analysis
1 parent 492db31 commit d84a262

File tree

5 files changed

+24
-11
lines changed

5 files changed

+24
-11
lines changed

src/Application/HydraScript.Application.CodeGeneration/Impl/ValueFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public IValue Create(ValueDto dto) =>
2222
};
2323

2424
public Name CreateName(string id, bool env = false) =>
25-
env ? new Name(id, EnvFrame) : new Name(id, CurrentFrame);
25+
env ? new EnvName(id, EnvFrame) : new Name(id, CurrentFrame);
2626

2727
private CurrentFrame CurrentFrame { get; } = new(frameContext);
2828

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ internal class SemanticChecker : VisitorBase<IAbstractSyntaxTreeNode, Type>,
2424
IVisitor<ReturnStatement, Type>,
2525
IVisitor<ExpressionStatement, Type>,
2626
IVisitor<IdentifierReference, Type>,
27+
IVisitor<EnvVarReference, Type>,
2728
IVisitor<Literal, Type>,
2829
IVisitor<ImplicitLiteral, Type>,
2930
IVisitor<ArrayLiteral, ArrayType>,
@@ -154,6 +155,8 @@ public Type Visit(IdentifierReference visitable)
154155
return symbol?.Type ?? throw new UnknownIdentifierReference(visitable);
155156
}
156157

158+
public Type Visit(EnvVarReference visitable) => "string";
159+
157160
public Type Visit(Literal visitable) =>
158161
visitable.Type.Accept(_typeBuilder);
159162

@@ -341,9 +344,11 @@ public Type Visit(AssignmentExpression visitable)
341344
return destinationType;
342345
}
343346

344-
var symbol =
345-
_symbolTables[visitable.Scope].FindSymbol(new VariableSymbolId(visitable.Destination.Id)) ??
346-
throw new UnknownIdentifierReference(visitable.Destination.Id);
347+
// здесь может быть переменная программы, а может быть переменная среды
348+
var symbol = visitable.Destination.Id.ToValueDto().Type is ValueDtoType.Name
349+
? _symbolTables[visitable.Scope].FindSymbol(new VariableSymbolId(visitable.Destination.Id)) ??
350+
throw new UnknownIdentifierReference(visitable.Destination.Id)
351+
: new VariableSymbol(visitable.Destination.Id, "string");
347352

348353
if (symbol.ReadOnly)
349354
throw new AssignmentToConst(visitable.Destination.Id);

src/Domain/HydraScript.Domain.BackEnd/Impl/Frames/EnvFrame.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ public sealed class EnvFrame(IEnvironmentVariableProvider provider) : IFrame
44
{
55
public object? this[string id]
66
{
7-
get => provider.GetEnvironmentVariable(id[1..]);
8-
set => provider.SetEnvironmentVariable(id[1..], value?.ToString());
7+
get => provider.GetEnvironmentVariable(id);
8+
set => provider.SetEnvironmentVariable(id, value?.ToString());
99
}
1010
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using HydraScript.Domain.BackEnd.Impl.Frames;
2+
3+
namespace HydraScript.Domain.BackEnd.Impl.Values;
4+
5+
public class EnvName(string id, EnvFrame frame) : Name(id, frame)
6+
{
7+
public override string ToString() => $"${Id}";
8+
}

src/Domain/HydraScript.Domain.BackEnd/Impl/Values/Name.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@ namespace HydraScript.Domain.BackEnd.Impl.Values;
22

33
public class Name(string id, IFrame frame) : IValue
44
{
5-
private readonly string _id = id;
5+
protected string Id { get; } = id;
66
private IFrame _frame = frame;
77

8-
public object? Get() => _frame[_id];
8+
public object? Get() => _frame[Id];
99

10-
public void Set(object? value) => _frame[_id] = value;
10+
public void Set(object? value) => _frame[Id] = value;
1111

1212
public void SetFrame(IFrame newFrame) => _frame = newFrame;
1313

14-
public override string ToString() => _id;
14+
public override string ToString() => Id;
1515

1616
public bool Equals(IValue? other) =>
1717
other is Name that &&
18-
_id == that._id;
18+
Id == that.Id;
1919

2020
internal static IFrame NullFrameInstance { get; } = new NullFrame();
2121

0 commit comments

Comments
 (0)