-
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.
Merge branch 'ast-semantic-analysis' of github.com:pile-lang/rusted-p…
…ile into ast-semantic-analysis Signed-off-by: Felipi Lima Matozinho <[email protected]>
- Loading branch information
Showing
16 changed files
with
297 additions
and
81 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,13 @@ | ||
\ vim: ft=forth | ||
|
||
1 2 < dump | ||
2 1 < dump | ||
2.1 1 > dump | ||
\ 1 2 < dump | ||
\ 2 1 < dump | ||
\ 2.1 1 > dump | ||
|
||
2 1 < if | ||
1 dump | ||
else | ||
2 dump | ||
end | ||
|
||
3 dump |
Binary file not shown.
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use clap::{Args, ValueEnum}; | ||
use miette::Result as MietteResult; | ||
|
||
use crate::{ | ||
codegen::{self, CodeGeneratorTarget}, | ||
grammar, lexer, | ||
parser::SLR::SLR, | ||
semantic::SemanticAnalyzer, | ||
}; | ||
|
||
use super::PileCompiler; | ||
|
||
#[allow(clippy::upper_case_acronyms)] | ||
#[derive(ValueEnum, Clone)] | ||
pub enum Codegen { | ||
VM, | ||
LLVM, | ||
} | ||
|
||
#[derive(Args)] | ||
pub struct Compile { | ||
#[arg(required = true, short, long)] | ||
pub filename: String, | ||
|
||
#[arg(short, long, default_value = "vm")] | ||
pub codegen: Codegen, | ||
|
||
#[arg(short, long, default_value = "output")] | ||
pub output: String, | ||
} | ||
|
||
impl PileCompiler { | ||
pub fn compile( | ||
Compile { | ||
filename, | ||
codegen, | ||
output, | ||
}: &Compile, | ||
) -> MietteResult<(), Box<dyn std::error::Error>> { | ||
// Lexer | ||
let lang_contents = std::fs::read_to_string(filename)?; | ||
let tokens = lexer::generate::compute_tokens(&lang_contents)?; | ||
|
||
// Parser | ||
let glc_contents = std::fs::read_to_string("assets/glc/lang.glc")?; | ||
let mut glc = grammar::parser::parse(&glc_contents)?; | ||
|
||
glc.compute_follow_set().expand(); | ||
|
||
let abstract_syntax_tree = SLR::new(glc) | ||
.parse(tokens, &lang_contents)? | ||
.ok_or("Failed to parse")?; | ||
|
||
SemanticAnalyzer::new(lang_contents).analyze(&abstract_syntax_tree)?; | ||
|
||
match codegen { | ||
Codegen::VM => codegen::code_generator(CodeGeneratorTarget::VirtualMachine), | ||
Codegen::LLVM => codegen::code_generator(CodeGeneratorTarget::LLVM), | ||
} | ||
.generate(abstract_syntax_tree, output.clone())?; | ||
|
||
Ok(()) | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
use clap::{Parser, Subcommand}; | ||
|
||
pub mod compile; | ||
pub mod run; | ||
|
||
#[derive(Parser)] | ||
#[command(author, version, about, long_about = None)] | ||
#[command(propagate_version = true)] | ||
pub struct Cli { | ||
#[command(subcommand)] | ||
pub command: Commands, | ||
} | ||
|
||
#[derive(Subcommand)] | ||
pub enum Commands { | ||
Compile(compile::Compile), | ||
Run(run::Run), | ||
} | ||
|
||
pub struct PileCompiler; |
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
use clap::Args; | ||
|
||
use crate::interpreter::vm; | ||
use miette::Result as MietteResult; | ||
|
||
use super::PileCompiler; | ||
|
||
#[derive(Args)] | ||
pub struct Run { | ||
#[arg(required = true, short, long)] | ||
pub filename: String, | ||
} | ||
|
||
impl PileCompiler { | ||
pub fn run(Run { filename }: &Run) -> MietteResult<(), Box<dyn std::error::Error>> { | ||
vm::VMInterpreter::run(filename)?; | ||
|
||
Ok(()) | ||
} | ||
} |
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
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
Oops, something went wrong.