-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8da9aba
commit 683c1dc
Showing
24 changed files
with
168 additions
and
168 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions
4
...otype/analyzer/deprecated/lol_analyzer.py → compiler/analyzer/deprecated/lol_analyzer.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
prototype/analyzer/new_lol_analyzer.py → compiler/analyzer/new_lol_analyzer.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
26 changes: 13 additions & 13 deletions
26
prototype/examples/fibonacci.lol → compiler/examples/fibonacci.lol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
### Recursive Fibonacci Sequence | ||
module io = import("stdio.h"); | ||
|
||
function fibonacci(n: int64) -> int64 { | ||
return fibonacci(n - 1) + fibonacci(n - 2); | ||
} | ||
|
||
function main() -> int64 { | ||
let result: int64 = fibonacci(10); | ||
# I do not like copying the printf semantics from C... | ||
io::stdout("fibonacci(10) = %l\n", result); | ||
return 0; | ||
} | ||
### Recursive Fibonacci Sequence | ||
module io = import("stdio.h"); | ||
|
||
function fibonacci(n: int64) -> int64 { | ||
return fibonacci(n - 1) + fibonacci(n - 2); | ||
} | ||
|
||
function main() -> int64 { | ||
let result: int64 = fibonacci(10); | ||
# I do not like copying the printf semantics from C... | ||
io::stdout("fibonacci(10) = %l\n", result); | ||
return 0; | ||
} |
12 changes: 6 additions & 6 deletions
12
prototype/examples/helloworld.lol → compiler/examples/helloworld.lol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
### Basic hello world | ||
module io = import("stdio.h"); | ||
|
||
function main() -> i32 { | ||
io::printf("Hello, World!\n"); | ||
return 0; | ||
### Basic hello world | ||
module io = import("stdio.h"); | ||
|
||
function main() -> i32 { | ||
io::printf("Hello, World!\n"); | ||
return 0; | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,133 +1,133 @@ | ||
import argparse | ||
import json | ||
import os | ||
import time | ||
from typing import Any, Dict, List | ||
|
||
from prototype.lexer.lol_lexer_types import Token | ||
from prototype.parser.lol_parser_token_stream import TokenStream | ||
from prototype.parser.lol_parser_types import ASTNode | ||
|
||
from prototype.lexer.lol_lexer import tokenize | ||
from prototype.parser.lol_parser import parse | ||
from prototype.analyzer.new_lol_analyzer import analyze | ||
from prototype.emitter.lol_emitter import emit_c | ||
|
||
|
||
class LolSymbol: | ||
def __init__(self): | ||
self.type: Any = None | ||
self.definition: Any = None | ||
|
||
def to_dict(self) -> Dict[str, Any]: | ||
return {"type": self.type, } | ||
|
||
|
||
class LolModule: | ||
def __init__(self): | ||
# Metadata | ||
self.init_timestamp = time.time() | ||
|
||
self.text: str = "" | ||
self.tokens: List[Token] = [] | ||
self.ast: List[ASTNode] = [] | ||
self.symbol_table: Dict[str, LolSymbol] = {} | ||
|
||
def read_file(self, file_name: str): | ||
assert isinstance(file_name, str) | ||
with open(file_name) as f: | ||
self.text = f.read() | ||
|
||
############################################################################ | ||
### LEXER | ||
############################################################################ | ||
|
||
def run_lexer(self): | ||
assert self.text != "", "LolModule" | ||
assert self.tokens == [] | ||
|
||
self.tokens = tokenize(self.text) | ||
|
||
def save_lexer_output_only(self, output_dir: str): | ||
file_name: str = f"{output_dir}/{self.init_timestamp}-lexer-output-only.json" | ||
with open(file_name, "w") as f: | ||
json.dump({"lexer-output": [x.to_dict() for x in self.tokens]}, f, indent=4) | ||
|
||
############################################################################ | ||
### PARSER | ||
############################################################################ | ||
|
||
def run_parser(self): | ||
assert self.tokens != [] | ||
|
||
stream = TokenStream(self.tokens, self.text) | ||
self.ast = parse(stream) | ||
|
||
def save_parser_output_only(self, output_dir: str): | ||
file_name: str = f"{output_dir}/{self.init_timestamp}-parser-output-only.json" | ||
with open(file_name, "w") as f: | ||
json.dump({"parser-output": [x.to_dict() for x in self.ast]}, f, indent=4) | ||
|
||
############################################################################ | ||
### ANALYZER | ||
############################################################################ | ||
|
||
def run_analyzer(self): | ||
self.symbol_table = analyze(self.ast, self.text) | ||
|
||
def save_analyzer_output_only(self, output_dir: str): | ||
file_name: str = f"{output_dir}/{self.init_timestamp}-analyzer-output-only.json" | ||
with open(file_name, "w") as f: | ||
json.dump({"analyzer-output": {x: y.to_dict() for x, y in self.symbol_table.module_symbol_table.items()}}, f, indent=4) | ||
|
||
############################################################################ | ||
### EMITTER | ||
############################################################################ | ||
|
||
def run_emitter(self): | ||
# TODO: Make this in the __init__function | ||
self.code = emit_c(self.symbol_table) | ||
|
||
def save_emitter_output_only(self, output_dir: str): | ||
file_name: str = f"{output_dir}/{self.init_timestamp}-emitter-output-only.json" | ||
with open(file_name, "w") as f: | ||
json.dump({"emitter-output": self.code}, f, indent=4) | ||
|
||
|
||
|
||
def main() -> None: | ||
parser = argparse.ArgumentParser() | ||
# TODO(dchu): make this accept multiple file names or folders. Also accept | ||
# a full configuration file. | ||
parser.add_argument( | ||
"-i", "--input", type=str, required=True, help="Input file name" | ||
) | ||
parser.add_argument( | ||
"-o", "--output", type=str, default=None, help="Output directory name" | ||
) | ||
args = parser.parse_args() | ||
|
||
# I explicitly extract the names because otherwise one may be tempted to | ||
# pass the 'args' namespace, which is always confusing. | ||
input_file = args.input | ||
output_dir = args.output | ||
|
||
module = LolModule() | ||
# Assume input_file is not None because it is required | ||
module.read_file(input_file) | ||
# Make empty output dir if it doesn't exist | ||
if not os.path.exists(output_dir): | ||
os.mkdir(output_dir) | ||
|
||
module.run_lexer() | ||
module.save_lexer_output_only(output_dir) | ||
module.run_parser() | ||
module.save_parser_output_only(output_dir) | ||
module.run_analyzer() | ||
module.save_analyzer_output_only(output_dir) | ||
module.run_emitter() | ||
module.save_emitter_output_only(output_dir) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() | ||
import argparse | ||
import json | ||
import os | ||
import time | ||
from typing import Any, Dict, List | ||
|
||
from compiler.lexer.lol_lexer_types import Token | ||
from compiler.parser.lol_parser_token_stream import TokenStream | ||
from compiler.parser.lol_parser_types import ASTNode | ||
|
||
from compiler.lexer.lol_lexer import tokenize | ||
from compiler.parser.lol_parser import parse | ||
from compiler.analyzer.new_lol_analyzer import analyze | ||
from compiler.emitter.lol_emitter import emit_c | ||
|
||
|
||
class LolSymbol: | ||
def __init__(self): | ||
self.type: Any = None | ||
self.definition: Any = None | ||
|
||
def to_dict(self) -> Dict[str, Any]: | ||
return {"type": self.type, } | ||
|
||
|
||
class LolModule: | ||
def __init__(self): | ||
# Metadata | ||
self.init_timestamp = time.time() | ||
|
||
self.text: str = "" | ||
self.tokens: List[Token] = [] | ||
self.ast: List[ASTNode] = [] | ||
self.symbol_table: Dict[str, LolSymbol] = {} | ||
|
||
def read_file(self, file_name: str): | ||
assert isinstance(file_name, str) | ||
with open(file_name) as f: | ||
self.text = f.read() | ||
|
||
############################################################################ | ||
### LEXER | ||
############################################################################ | ||
|
||
def run_lexer(self): | ||
assert self.text != "", "LolModule" | ||
assert self.tokens == [] | ||
|
||
self.tokens = tokenize(self.text) | ||
|
||
def save_lexer_output_only(self, output_dir: str): | ||
file_name: str = f"{output_dir}/{self.init_timestamp}-lexer-output-only.json" | ||
with open(file_name, "w") as f: | ||
json.dump({"lexer-output": [x.to_dict() for x in self.tokens]}, f, indent=4) | ||
|
||
############################################################################ | ||
### PARSER | ||
############################################################################ | ||
|
||
def run_parser(self): | ||
assert self.tokens != [] | ||
|
||
stream = TokenStream(self.tokens, self.text) | ||
self.ast = parse(stream) | ||
|
||
def save_parser_output_only(self, output_dir: str): | ||
file_name: str = f"{output_dir}/{self.init_timestamp}-parser-output-only.json" | ||
with open(file_name, "w") as f: | ||
json.dump({"parser-output": [x.to_dict() for x in self.ast]}, f, indent=4) | ||
|
||
############################################################################ | ||
### ANALYZER | ||
############################################################################ | ||
|
||
def run_analyzer(self): | ||
self.symbol_table = analyze(self.ast, self.text) | ||
|
||
def save_analyzer_output_only(self, output_dir: str): | ||
file_name: str = f"{output_dir}/{self.init_timestamp}-analyzer-output-only.json" | ||
with open(file_name, "w") as f: | ||
json.dump({"analyzer-output": {x: y.to_dict() for x, y in self.symbol_table.module_symbol_table.items()}}, f, indent=4) | ||
|
||
############################################################################ | ||
### EMITTER | ||
############################################################################ | ||
|
||
def run_emitter(self): | ||
# TODO: Make this in the __init__function | ||
self.code = emit_c(self.symbol_table) | ||
|
||
def save_emitter_output_only(self, output_dir: str): | ||
file_name: str = f"{output_dir}/{self.init_timestamp}-emitter-output-only.json" | ||
with open(file_name, "w") as f: | ||
json.dump({"emitter-output": self.code}, f, indent=4) | ||
|
||
|
||
|
||
def main() -> None: | ||
parser = argparse.ArgumentParser() | ||
# TODO(dchu): make this accept multiple file names or folders. Also accept | ||
# a full configuration file. | ||
parser.add_argument( | ||
"-i", "--input", type=str, required=True, help="Input file name" | ||
) | ||
parser.add_argument( | ||
"-o", "--output", type=str, default=None, help="Output directory name" | ||
) | ||
args = parser.parse_args() | ||
|
||
# I explicitly extract the names because otherwise one may be tempted to | ||
# pass the 'args' namespace, which is always confusing. | ||
input_file = args.input | ||
output_dir = args.output | ||
|
||
module = LolModule() | ||
# Assume input_file is not None because it is required | ||
module.read_file(input_file) | ||
# Make empty output dir if it doesn't exist | ||
if not os.path.exists(output_dir): | ||
os.mkdir(output_dir) | ||
|
||
module.run_lexer() | ||
module.save_lexer_output_only(output_dir) | ||
module.run_parser() | ||
module.save_parser_output_only(output_dir) | ||
module.run_analyzer() | ||
module.save_analyzer_output_only(output_dir) | ||
module.run_emitter() | ||
module.save_emitter_output_only(output_dir) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.