Skip to content
This repository was archived by the owner on Jan 11, 2026. It is now read-only.

Commit 11d5953

Browse files
committed
ast
1 parent 6110576 commit 11d5953

File tree

17 files changed

+1260
-321
lines changed

17 files changed

+1260
-321
lines changed

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22

33
build:
44
@$(MAKE) -C compiler build $(MAKEOVERRIDES)
5-
LEXER_PATH=$$(dirname $(abspath $(lastword $(MAKEFILE_LIST))))/compiler/bin/Release/net9.0/CompilersApp \
5+
ANALYZER_PATH=$$(dirname $(abspath $(lastword $(MAKEFILE_LIST))))/compiler/bin/Release/net9.0/CompilersApp \
66
$(MAKE) -C frontend build $(MAKEOVERRIDES)
77

88
dev:
99
(nodemon --watch compiler --ext cs --ignore compiler/obj \
1010
--exec "make -C compiler build && echo 'Rebuilt compiler'") & \
1111
echo "Starting frontend development server..."; \
12-
LEXER_PATH=$$(dirname $(abspath $(lastword $(MAKEFILE_LIST))))/compiler/bin/Release/net9.0/CompilersApp \
12+
ANALYZER_PATH=$$(dirname $(abspath $(lastword $(MAKEFILE_LIST))))/compiler/bin/Release/net9.0/CompilersApp \
1313
$(MAKE) -C frontend dev $(MAKEOVERRIDES) -- $(ARGS) & \
1414
wait
1515

1616
start:
17-
LEXER_PATH=$$(dirname $(abspath $(lastword $(MAKEFILE_LIST))))/compiler/bin/Release/net9.0/CompilersApp \
17+
ANALYZER_PATH=$$(dirname $(abspath $(lastword $(MAKEFILE_LIST))))/compiler/bin/Release/net9.0/CompilersApp \
1818
$(MAKE) -C frontend start $(MAKEOVERRIDES) -- $(ARGS)
1919

2020
install:

compiler/Program.cs

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
using System.Text;
2+
using System.Text.Json;
3+
using System.Text.Json.Serialization;
4+
15
public class Program
26
{
37
public static void Main(string[] args)
@@ -71,18 +75,37 @@ public static void Main(string[] args)
7175
return;
7276
}
7377

74-
Lexer scanner = new Lexer(sourceCode);
75-
IReadOnlyList<Token> tokens = scanner.ScanTokens();
78+
TextWriter writer = outputFile != null
79+
? new StreamWriter(outputFile, false, new UTF8Encoding(false))
80+
: Console.Out;
7681

77-
using (var writer = outputFile != null ? new StreamWriter(outputFile) : Console.Out)
82+
try
7883
{
79-
var options = new System.Text.Json.JsonSerializerOptions
84+
var tokens = new Lexer(sourceCode).ScanTokens();
85+
var analyzer = new Analyzer();
86+
var ast = analyzer.analyze(tokens);
87+
var options = new JsonSerializerOptions
8088
{
81-
WriteIndented = true,
82-
Converters = { new System.Text.Json.Serialization.JsonStringEnumConverter() }
89+
Converters = { new JsonStringEnumConverter() },
90+
MaxDepth = 2048
8391
};
84-
var json = System.Text.Json.JsonSerializer.Serialize(tokens, options);
92+
var json = JsonSerializer.Serialize(ast, options);
8593
writer.WriteLine(json);
8694
}
95+
catch (SyntaxError e)
96+
{
97+
Console.Error.WriteLine(e.Message);
98+
Environment.ExitCode = 1;
99+
}
100+
catch (Exception e)
101+
{
102+
Console.Error.WriteLine(e.Message);
103+
Environment.ExitCode = 1;
104+
}
105+
finally
106+
{
107+
if (!ReferenceEquals(writer, Console.Out))
108+
writer.Dispose();
109+
}
87110
}
88111
}

0 commit comments

Comments
 (0)