-
Notifications
You must be signed in to change notification settings - Fork 1
/
dartox.dart
74 lines (62 loc) · 2.06 KB
/
dartox.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import 'dart:io';
import 'package:args/args.dart';
import 'package:dartox/src/token.dart';
import 'package:dartox/src/scanner.dart';
import 'package:dartox/src/error.dart';
import 'package:dartox/src/expr.dart';
import 'package:dartox/src/parser.dart';
import 'package:dartox/src/statement.dart';
import 'package:dartox/src/ast_printer.dart';
import 'package:dartox/src/interpreter.dart';
import 'package:dartox/src/resolver.dart';
final ErrorReporter errorReporter = ErrorReporter();
final Interpreter interpreter = Interpreter(errorReporter);
main(List<String> args) {
final ArgParser argParser = ArgParser()
..addOption('source', abbr: 's', help: "The file you want to compile.");
ArgResults argResults = argParser.parse(args);
if (argResults.arguments.length > 1) {
// Too many arguments, help user with what they need to input.
print("${argParser.usage}");
exit(64);
} else if (argResults.arguments.length == 1) {
_run(argResults['file']);
if (errorReporter.hadError) exit(65);
if (errorReporter.hadRuntimeError) exit(70);
} else {
// Run interactive prompt
_runPrompt();
// If user makes error, we shouldn't close the entire session.
errorReporter.clear();
}
}
void _runPrompt() {
for (;;) {
stdout.write("> ");
_run(stdin.readLineSync());
}
}
void _run(String source) {
Scanner scanner = Scanner(source);
List<Token> tokens = scanner.scanTokens();
Parser parser = Parser(tokens, errorReporter);
List<Statement> statements = parser.parse();
// Stop if there is a syntax error.
if (errorReporter.hadError) return;
Resolver resolver = Resolver(interpreter, errorReporter);
resolver.resolveStatements(statements);
// Stop if there was a resolution error.
if (errorReporter.hadError) return;
// Interpret the AST created.
interpreter.interpret(statements);
}
/// Debug method to print all tokens.
void _printTokens(List<Token> tokens) {
for (Token token in tokens) {
print(token);
}
}
/// Debug method to print the valid AST.
void _printAst(Expr expr) {
print(AstPrinter().print(expr));
}