-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Broke up code into files, expanded grammar
And basic parser test
- Loading branch information
1 parent
cc14329
commit b91e1c0
Showing
7 changed files
with
156 additions
and
56 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
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,4 @@ | ||
#[derive(Debug, Clone, PartialEq, Eq)] | ||
pub enum Node { | ||
Print(String) | ||
} |
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,7 @@ | ||
use crate::ast::Node; | ||
|
||
pub fn evaluate(ast: &Node) { | ||
match ast { | ||
Node::Print(x) => println!("{}", x), | ||
} | ||
} |
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,51 +1,13 @@ | ||
extern crate pest; | ||
extern crate pest_derive; | ||
|
||
use pest::Parser; | ||
|
||
#[derive(Debug)] | ||
pub enum Node { | ||
Print(String) | ||
} | ||
|
||
#[derive(pest_derive::Parser)] | ||
#[grammar = "grammar.pest"] | ||
struct ForceParser; | ||
|
||
fn parse(source: &str) -> Result<Vec<Node>, pest::error::Error<Rule>> { | ||
let mut ast = vec![]; | ||
let pairs = ForceParser::parse(Rule::Program, source)?; | ||
for pair in pairs { | ||
if let Rule::Main = pair.as_rule() { | ||
ast.push(build_ast(pair)); | ||
} | ||
} | ||
Ok(ast) | ||
} | ||
|
||
fn build_ast(pair: pest::iterators::Pair<Rule>) -> Node { | ||
match pair.as_rule() { | ||
Rule::Main => { | ||
let mut pair = pair.into_inner(); | ||
let string = pair.next().unwrap(); | ||
Node::Print(string.as_span().as_str().to_string()) | ||
} | ||
unknown => panic!("Unknown expr: {:?}", unknown), | ||
} | ||
} | ||
|
||
fn evaluate(ast: &Node) { | ||
match ast { | ||
Node::Print(x) => println!("{}", x), | ||
} | ||
} | ||
mod ast; | ||
mod parser; | ||
mod interpreter; | ||
|
||
fn main() { | ||
let source = r#" | ||
Do it | ||
The Sacred Texts! "Hello there" | ||
May The Force be with you | ||
"#; | ||
let ast = parse(source); | ||
evaluate(ast.unwrap().first().unwrap()); | ||
let ast = parser::parse(source); | ||
interpreter::evaluate(ast.unwrap().first().unwrap()); | ||
} |
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,54 @@ | ||
extern crate pest; | ||
extern crate pest_derive; | ||
|
||
use pest::Parser; | ||
|
||
use crate::ast::Node; | ||
|
||
#[derive(pest_derive::Parser)] | ||
#[grammar = "grammar.pest"] | ||
struct ForceParser; | ||
|
||
pub fn parse(source: &str) -> Result<Vec<Node>, pest::error::Error<Rule>> { | ||
let mut ast = vec![]; | ||
let pairs = ForceParser::parse(Rule::Program, source)?; | ||
for pair in pairs { | ||
if let Rule::Main = pair.as_rule() { | ||
ast.push(build_ast(pair)); | ||
} | ||
} | ||
Ok(ast) | ||
} | ||
|
||
fn build_ast(pair: pest::iterators::Pair<Rule>) -> Node { | ||
match pair.as_rule() { | ||
Rule::Main => { | ||
let mut pair = pair.into_inner(); | ||
let string = pair.next().unwrap(); | ||
Node::Print(string.as_span().as_str().to_string()) | ||
} | ||
unknown => panic!("Unknown expr: {:?}", unknown), | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
// Parser tests | ||
#[test] | ||
fn hello_there() { | ||
let source = r#" | ||
Do it | ||
The Sacred Texts! "Hello there" | ||
May The Force be with you | ||
"#; | ||
let hello_there = parse(source); | ||
assert!(hello_there.is_ok()); | ||
|
||
assert_eq!( | ||
hello_there.clone().unwrap(), | ||
vec![Node::Print("\"Hello there\"".to_string())] | ||
); | ||
} | ||
} |