-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Raw canonical ast separation #10
Open
zypeh
wants to merge
17
commits into
master
Choose a base branch
from
raw-canonical-ast-separation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
547fac6
Changed List to Pair, and also re-implemented type_of
8da73c2
Renaming & test case added
zypeh 5e3393f
review: bugfixes tuple syntax and self arg
zypeh 657374b
Merge pull request #4 from topoi-lang/change-list-to-pair-zy
wongjiahau 0d4e29b
Rename T-Ty to T
b24d0bc
wip: tokenizer implementation
zypeh 4d7bb7c
wip: whitespace token parsing
zypeh e077e0b
Sexpression lib
zypeh 518a40b
chore: move tests to corresponding file
zypeh 9a88c4a
wip: development
zypeh e6dd910
feat: tokenizer finish
zypeh 49266d9
chore: remove unused dependency
zypeh 0b0a82e
wip: parser for s-expression
zypeh 77625b1
Pair instead of list
zypeh c45e99a
Done parser
zypeh 269ba9e
tests added
zypeh 71eaae7
Added sexpr_tokenizer_2.rs to demonstrate immutable code
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# sexpr | ||
|
||
> S Expression parser | ||
|
||
## Overview | ||
|
||
Note when developing this S expression parser: | ||
|
||
- Tokenization, a.k.a lexing. The input is transformed into a stream of tokens. Tokenization never fails, although the output may contain error tokens. |
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,69 +1,4 @@ | ||
pub mod node; | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::node::*; | ||
use crate::node::Node::*; | ||
|
||
#[test] | ||
fn atom_cmp() { | ||
assert_eq!(Atom("ratatouille"), Atom("ratatouille")); | ||
assert_ne!(Atom("ratatouille"), Atom("baguette")) | ||
} | ||
|
||
#[test] | ||
fn list_cmp() { | ||
assert_eq!( | ||
List(vec![Atom("ratatouille")]), | ||
List(vec![Atom("ratatouille")]) | ||
); | ||
|
||
assert_ne!( | ||
List(vec![Atom("ratatouille")]), | ||
List(vec![Atom("baguette")]) | ||
); | ||
} | ||
|
||
#[test] | ||
fn list_cons() { | ||
use crate::node::cons; | ||
assert_eq!( | ||
cons(vec![Atom("ratatouille")], "baguette"), | ||
List(vec![Atom("ratatouille"), Atom("baguette")]) | ||
) | ||
} | ||
|
||
#[test] | ||
fn list_car() { | ||
use crate::node::car; | ||
assert_eq!( | ||
car(vec![Atom("ratatouille"), Atom("baguette")]), | ||
Atom("ratatouille") | ||
); | ||
} | ||
|
||
#[test] | ||
fn list_cdr() { | ||
use crate::node::cdr; | ||
assert_eq!( | ||
cdr(vec![Atom("ratatouille"), Atom("baguette")]), | ||
List(vec![Atom("baguette")]) | ||
); | ||
|
||
assert_eq!( | ||
cdr(vec![ | ||
Atom("ratatouille"), | ||
Atom("baguette"), | ||
Atom("aubergine") | ||
]), | ||
List(vec![Atom("baguette"), Atom("aubergine")]) | ||
) | ||
} | ||
|
||
#[test] | ||
fn type_of_term() { | ||
assert_eq!(type_of(Atom("yo")), Type(0)); | ||
assert_eq!(type_of(List(vec![])), Type(0)); | ||
assert_eq!(type_of(Type(0)), Type(1)) | ||
} | ||
} | ||
pub mod sexpr_parser; | ||
pub mod sexpr_tokenizer; | ||
pub mod sexpr_tokenizer_2; | ||
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,37 +1,157 @@ | ||
#[derive(Clone, PartialEq, Debug)] | ||
pub enum Node { | ||
Atom(Atom), | ||
List(List), | ||
Type(i64) | ||
Atom(&'static str), | ||
PairNode(Pair<Node>), | ||
TypeNode(Type), | ||
Unit, | ||
} | ||
|
||
pub type Atom = &'static str; | ||
#[derive(Clone, PartialEq, Debug)] | ||
pub enum Type { | ||
Universe(i64), | ||
Atom, | ||
Pair(Pair<Type>), | ||
Unit, | ||
} | ||
|
||
// ===================================== | ||
// List | ||
// ===================================== | ||
pub type List = Vec<Node>; | ||
#[derive(Clone, PartialEq, Debug)] | ||
pub struct Pair<T>(pub Box<T>, pub Box<T>); | ||
|
||
pub fn cons(list: List, atom: Atom) -> Node { | ||
let mut cloned_list = list.clone(); | ||
cloned_list.push(Node::Atom(atom)); | ||
Node::List(cloned_list) | ||
} | ||
impl<T> Pair<T> { | ||
pub fn cons(node1: T, node2: T) -> Pair<T> { | ||
Pair(Box::new(node1), Box::new(node2)) | ||
} | ||
|
||
pub fn car(list: List) -> Node { | ||
list.first().unwrap().clone() | ||
} | ||
pub fn car(self) -> T { | ||
*self.0 | ||
} | ||
|
||
pub fn cdr(list: List) -> Node { | ||
let mut cloned_list = list.clone(); | ||
cloned_list.remove(0); | ||
Node::List(cloned_list) | ||
pub fn cdr(self) -> T { | ||
*self.1 | ||
} | ||
} | ||
|
||
pub fn type_of(node: Node) -> Node { | ||
pub fn type_of(node: Node) -> Type { | ||
match node { | ||
Node::Atom(_) => Node::Type(0), | ||
Node::List(_) => Node::Type(0), | ||
Node::Type(n) => Node::Type(n + 1) | ||
Node::Atom(_) => Type::Atom, | ||
Node::PairNode(p) => Type::Pair(Pair(Box::new(type_of(*p.0)), Box::new(type_of(*p.1)))), | ||
Node::TypeNode(Type::Universe(n)) => Type::Universe(n + 1), | ||
Node::TypeNode(_) => Type::Universe(0), | ||
Node::Unit => Type::Unit, | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use crate::node::Node::*; | ||
use crate::node::*; | ||
|
||
#[test] | ||
fn atom_cmp() { | ||
assert_eq!(Atom("ratatouille"), Atom("ratatouille")); | ||
assert_ne!(Atom("ratatouille"), Atom("baguette")) | ||
} | ||
|
||
#[test] | ||
fn pair_cmp() { | ||
assert_eq!( | ||
PairNode(Pair( | ||
Box::new(Atom("ratatouille")), | ||
Box::new(Atom("ratatouille")), | ||
)), | ||
PairNode(Pair( | ||
Box::new(Atom("ratatouille")), | ||
Box::new(Atom("ratatouille")), | ||
)) | ||
); | ||
|
||
assert_ne!( | ||
PairNode(Pair( | ||
Box::new(Atom("ratatouille")), | ||
Box::new(Atom("ratatouille")) | ||
)), | ||
PairNode(Pair( | ||
Box::new(Atom("ratatouille")), | ||
Box::new(Atom("baguette")), | ||
)) | ||
); | ||
|
||
assert_ne!( | ||
PairNode(Pair( | ||
Box::new(Atom("ratatouille")), | ||
Box::new(Atom("ratatouille")), | ||
)), | ||
PairNode(Pair( | ||
Box::new(Atom("baguette")), | ||
Box::new(Atom("ratatouille")), | ||
)) | ||
); | ||
|
||
assert_ne!( | ||
PairNode(Pair( | ||
Box::new(Atom("ratatouille")), | ||
Box::new(Atom("baguette")), | ||
)), | ||
PairNode(Pair(Box::new(Atom("baguette")), Box::new(Atom("baguette")),)) | ||
); | ||
|
||
assert_ne!( | ||
PairNode(Pair(Box::new(Atom("baguette")), Box::new(Atom("baguette")),)), | ||
PairNode(Pair( | ||
Box::new(Atom("baguette")), | ||
Box::new(Atom("ratatouille")), | ||
)) | ||
); | ||
} | ||
|
||
#[test] | ||
fn pair_cons() { | ||
assert_eq!( | ||
Pair::cons(Atom("ratatouille"), Atom("baguette")), | ||
Pair(Box::new(Atom("ratatouille")), Box::new(Atom("baguette")),) | ||
) | ||
} | ||
|
||
#[test] | ||
fn pair_car() { | ||
assert_eq!( | ||
Pair::car(Pair( | ||
Box::new(Atom("ratatouille")), | ||
Box::new(Atom("baguette")) | ||
)), | ||
Atom("ratatouille") | ||
); | ||
} | ||
|
||
#[test] | ||
fn pair_cdr() { | ||
assert_eq!( | ||
Pair::cdr(Pair( | ||
Box::new(Atom("ratatouille")), | ||
Box::new(Atom("baguette")), | ||
)), | ||
Atom("baguette") | ||
); | ||
|
||
assert_eq!( | ||
Pair::cdr(Pair( | ||
Box::new(Atom("ratatouille")), | ||
Box::new(Node::PairNode(Pair( | ||
Box::new(Atom("baguette")), | ||
Box::new(Atom("aubergine")), | ||
))), | ||
)), | ||
Node::PairNode(Pair( | ||
Box::new(Atom("baguette")), | ||
Box::new(Atom("aubergine")), | ||
)) | ||
) | ||
} | ||
} | ||
|
||
#[test] | ||
fn type_of_term() { | ||
use crate::node::type_of; | ||
use crate::node::Type; | ||
assert_eq!(type_of(Atom("yo")), Type::Atom); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not put these modules into a separate directory?