-
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
60364b0
commit 7e07f93
Showing
16 changed files
with
596 additions
and
42 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,6 @@ | ||
{ | ||
"cSpell.words": [ | ||
"astified", | ||
"ichop" | ||
] | ||
} |
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,9 @@ | ||
# Reference implementation for chop-lang | ||
|
||
 | ||
<p align="center"> | ||
<a href="https://travis-ci.org/lochbrunner/chop-compiler"><img src="https://travis-ci.org/lochbrunner/chop-compiler.svg?branch=master" alt="Build Status"></a> | ||
</p> | ||
|
||
|
||
|
||
See [Language Specifications](https://github.com/lochbrunner/chop-specs/blob/master/README.md) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Compiler | ||
|
||
## Architecture | ||
|
||
The Compiler for has these data flow chain | ||
|
||
* *Lexer* | ||
* *Parser* | ||
* *Generator* (also planed: *Injector* & *Checker*) | ||
* *Exporter* | ||
* Interpreter byte-code | ||
* planed: llvm | ||
* planed: chop IR | ||
|
||
## Challenges | ||
|
||
* How to propagate information (from *Generator* and *Injector*) back to the *Parser* and *Lexer*? | ||
|
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,16 @@ | ||
use crate::compiler::token::Token; | ||
|
||
#[derive(Debug, PartialEq)] | ||
pub struct Node { | ||
pub root: Token, | ||
pub args: Vec<Token>, | ||
} | ||
#[derive(Debug, PartialEq)] | ||
pub struct Statement { | ||
pub root: Node, | ||
} | ||
|
||
#[derive(Debug, PartialEq)] | ||
pub struct Ast { | ||
pub statements: Vec<Statement>, | ||
} |
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,109 @@ | ||
use crate::compiler::ast::Ast; | ||
use crate::compiler::token::TokenPayload; | ||
use crate::compiler::CompilerError; | ||
use crate::evaluation::bytecode::ByteCode; | ||
|
||
pub fn export(ast: Ast) -> Result<Vec<ByteCode>, CompilerError> { | ||
let mut bytecode = vec![]; | ||
for mut statement in ast.statements.into_iter() { | ||
match statement.root.root.token { | ||
TokenPayload::Ident(ident) => match ident.as_ref() { | ||
"stdout" => match statement.root.args.pop() { | ||
Some(arg) => { | ||
if !statement.root.args.is_empty() { | ||
return Err(CompilerError { | ||
location: statement.root.root.begin.clone(), | ||
msg: format!( | ||
"Exporter Error: Function {} has tow many arguments", | ||
ident | ||
), | ||
}); | ||
} | ||
match arg.token { | ||
TokenPayload::Int32(v) => { | ||
bytecode.push(ByteCode::PushInt32(v)); | ||
} | ||
_ => { | ||
return Err(CompilerError { | ||
location: statement.root.root.begin.clone(), | ||
msg: format!( | ||
"Exporter Error: Function {} expects an int as argument but got {:?}", | ||
ident, arg.token | ||
), | ||
}) | ||
} | ||
} | ||
bytecode.push(ByteCode::StdOut); | ||
} | ||
None => { | ||
return Err(CompilerError { | ||
location: statement.root.root.begin.clone(), | ||
msg: format!("Exporter Error: Function {} has no argument", ident), | ||
}) | ||
} | ||
}, | ||
_ => { | ||
return Err(CompilerError { | ||
location: statement.root.root.begin.clone(), | ||
msg: format!("Exporter Error: Unknown ident: {}", ident), | ||
}) | ||
} | ||
}, | ||
_ => { | ||
return Err(CompilerError { | ||
location: statement.root.root.begin.clone(), | ||
msg: format!( | ||
"Exporter Error: Unknown token {:?}", | ||
statement.root.root.token | ||
), | ||
}) | ||
} | ||
} | ||
} | ||
Ok(bytecode) | ||
} | ||
|
||
#[cfg(test)] | ||
mod specs { | ||
use super::*; | ||
use crate::compiler::ast::{Node, Statement}; | ||
use crate::compiler::token::{Location, Token, TokenPayload}; | ||
|
||
#[test] | ||
fn milestone_1() { | ||
let input = Ast { | ||
statements: vec![Statement { | ||
root: Node { | ||
root: Token { | ||
token: TokenPayload::Ident("stdout".to_owned()), | ||
begin: Location { | ||
line: 3, | ||
offset: 33, | ||
}, | ||
end: Location { | ||
line: 3, | ||
offset: 39, | ||
}, | ||
}, | ||
args: vec![Token { | ||
token: TokenPayload::Int32(42), | ||
begin: Location { | ||
line: 3, | ||
offset: 28, | ||
}, | ||
end: Location { | ||
line: 3, | ||
offset: 30, | ||
}, | ||
}], | ||
}, | ||
}], | ||
}; | ||
let actual = export(input); | ||
assert!(actual.is_ok()); | ||
let actual = actual.unwrap(); | ||
let expected = vec![ByteCode::PushInt32(42), ByteCode::StdOut]; | ||
|
||
assert_eq!(actual, expected); | ||
} | ||
} |
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,111 @@ | ||
use crate::compiler::ast::{Ast, Node, Statement}; | ||
use crate::compiler::token::TokenPayload; | ||
use crate::compiler::CompilerError; | ||
|
||
/// Fills out the macros and runs all the custom compiler stuff. | ||
pub fn generate(ast: Ast) -> Result<Ast, CompilerError> { | ||
let statements = ast | ||
.statements | ||
.into_iter() | ||
.map(|statement| match statement.root.root.token { | ||
TokenPayload::Pipe => Ok(Statement { | ||
root: Node { | ||
root: statement.root.args[1].clone(), | ||
args: vec![statement.root.args[0].clone()], | ||
}, | ||
}), | ||
_ => Err(CompilerError { | ||
location: statement.root.root.begin.clone(), | ||
msg: format!( | ||
"Generator Error: Token {:?} is not implemented yet!", | ||
statement.root.root.token | ||
), | ||
}), | ||
}) | ||
.collect::<Result<Vec<_>, _>>()?; | ||
Ok(Ast { statements }) | ||
} | ||
|
||
#[cfg(test)] | ||
mod specs { | ||
use super::*; | ||
use crate::compiler::token::{Location, Token, TokenPayload}; | ||
|
||
#[test] | ||
fn milestone_1() { | ||
let input = Ast { | ||
statements: vec![Statement { | ||
root: Node { | ||
root: Token { | ||
token: TokenPayload::Pipe, | ||
begin: Location { | ||
line: 3, | ||
offset: 31, | ||
}, | ||
end: Location { | ||
line: 3, | ||
offset: 32, | ||
}, | ||
}, | ||
args: vec![ | ||
Token { | ||
token: TokenPayload::Int32(42), | ||
begin: Location { | ||
line: 3, | ||
offset: 28, | ||
}, | ||
end: Location { | ||
line: 3, | ||
offset: 30, | ||
}, | ||
}, | ||
Token { | ||
token: TokenPayload::Ident("stdout".to_owned()), | ||
begin: Location { | ||
line: 3, | ||
offset: 33, | ||
}, | ||
end: Location { | ||
line: 3, | ||
offset: 39, | ||
}, | ||
}, | ||
], | ||
}, | ||
}], | ||
}; | ||
|
||
let actual = generate(input); | ||
assert!(actual.is_ok()); | ||
let actual = actual.unwrap(); | ||
let expected = Ast { | ||
statements: vec![Statement { | ||
root: Node { | ||
root: Token { | ||
token: TokenPayload::Ident("stdout".to_owned()), | ||
begin: Location { | ||
line: 3, | ||
offset: 33, | ||
}, | ||
end: Location { | ||
line: 3, | ||
offset: 39, | ||
}, | ||
}, | ||
args: vec![Token { | ||
token: TokenPayload::Int32(42), | ||
begin: Location { | ||
line: 3, | ||
offset: 28, | ||
}, | ||
end: Location { | ||
line: 3, | ||
offset: 30, | ||
}, | ||
}], | ||
}, | ||
}], | ||
}; | ||
assert_eq!(actual, expected); | ||
} | ||
} |
Oops, something went wrong.