-
Notifications
You must be signed in to change notification settings - Fork 3
Initial interpreter structure #49
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
Merged
Merged
Changes from all commits
Commits
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 hidden or 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,8 +1,2 @@ | ||
| x = 2; | ||
| y = 3; | ||
| add = create_blob("./target/x86_64-unknown-none/addblob"); | ||
| add_x_y = create_application_thunk(create_tree(add, x, y)); | ||
| z = 1; | ||
| sum_x_y = create_strict_encode(add_x_y) | ||
| add_xy_z = create_application_thunk(create_tree(add, sum_x_y, z)); | ||
| eval(create_strict_encode(add_xy_z)); | ||
| (let ((add @"./target/x86_64-unknown-none/addblob")) | ||
| !*(add !*(add 2 3) 1)) |
This file contains hidden or 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,57 +1,27 @@ | ||
| a = 1; | ||
| b = 2; | ||
| c = 3; | ||
| d = 4; | ||
| e = 5; | ||
| f = 6; | ||
| g = 7; | ||
| h = 8; | ||
| i = 9; | ||
| j = 10; | ||
| k = 11; | ||
| l = 12; | ||
| m = 13; | ||
| n = 14; | ||
| o = 15; | ||
| p = 16; | ||
|
|
||
| add = create_blob("./target/x86_64-unknown-none/addblob"); | ||
|
|
||
| ab = create_application_thunk(create_tree(add, a, b)); | ||
| cd = create_application_thunk(create_tree(add, c, d)); | ||
| ef = create_application_thunk(create_tree(add, e, f)); | ||
| gh = create_application_thunk(create_tree(add, g, h)); | ||
| ij = create_application_thunk(create_tree(add, i, j)); | ||
| kl = create_application_thunk(create_tree(add, k, l)); | ||
| mn = create_application_thunk(create_tree(add, m, n)); | ||
| op = create_application_thunk(create_tree(add, o, p)); | ||
|
|
||
| sum_ab = create_strict_encode(ab); | ||
| sum_cd = create_strict_encode(cd); | ||
| sum_ef = create_strict_encode(ef); | ||
| sum_gh = create_strict_encode(gh); | ||
| sum_ij = create_strict_encode(ij); | ||
| sum_kl = create_strict_encode(kl); | ||
| sum_mn = create_strict_encode(mn); | ||
| sum_op = create_strict_encode(op); | ||
|
|
||
| lefti = create_application_thunk(create_tree(add, sum_ab, sum_cd)); | ||
| righti = create_application_thunk(create_tree(add, sum_ef, sum_gh)); | ||
| leftr= create_application_thunk(create_tree(add, sum_ij, sum_kl)); | ||
| rightr = create_application_thunk(create_tree(add, sum_mn, sum_op)); | ||
|
|
||
| sum_lefti = create_strict_encode(lefti); | ||
| sum_righti = create_strict_encode(righti); | ||
| sum_leftr = create_strict_encode(leftr); | ||
| sum_rightr = create_strict_encode(rightr); | ||
|
|
||
| left = create_application_thunk(create_tree(add, sum_lefti, sum_leftr)); | ||
| right = create_application_thunk(create_tree(add, sum_righti, sum_rightr)); | ||
|
|
||
| sum_left = create_strict_encode(left); | ||
| sum_right = create_strict_encode(right); | ||
|
|
||
| final = create_application_thunk(create_tree(add, sum_left, sum_right)); | ||
|
|
||
| eval(create_strict_encode(final)); | ||
|
|
||
| (let ((add @"./target/x86_64-unknown-none/addblob") | ||
| (a 1) (b 2) (c 3) (d 4) (e 5) (f 6) (g 7) | ||
| (h 8) (i 9) (j 10) (k 11) (l 12) (m 13) | ||
| (n 14) (o 15) (p 16)) | ||
| !*(add | ||
| !*(add | ||
| !*(add | ||
| !*(add a b) | ||
| !*(add c d) | ||
| ) | ||
| !*(add | ||
| !*(add e f) | ||
| !*(add g h) | ||
| ) | ||
| ) | ||
| !*(add | ||
| !*(add | ||
| !*(add i j) | ||
| !*(add k l) | ||
| ) | ||
| !*(add | ||
| !*(add m n) | ||
| !*(add o p) | ||
| ) | ||
| ) | ||
| ) | ||
| ) |
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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,34 @@ | ||
| {- | ||
| -- The whole file is interpreted as a single expression. Expressions can be comprised of: | ||
|
|
||
| -- Integer Literals | ||
| 42 | ||
|
|
||
| -- String Literals | ||
| "hello" | ||
|
|
||
| -- Trees | ||
| (1 2 3) | ||
|
|
||
| -- References | ||
| &"hello" | ||
|
|
||
| -- Identification Thunks | ||
| ^&4 | ||
|
|
||
| -- Application Thunks | ||
| *(1 2) | ||
|
|
||
| -- Strict Encodes | ||
| !^&2 | ||
|
|
||
| -- Let bindings that tie expressions to variables | ||
| (let ((x 42)) x) | ||
|
|
||
| -- @ path executables | ||
| @"./target/x86_64-unknown-none/addblob" | ||
|
|
||
| -- Primitives (To be implemented) | ||
| $interpret | ||
|
|
||
| -} |
This file contains hidden or 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 hidden or 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,17 @@ | ||
| use kernel::prelude::Box; | ||
|
|
||
| pub trait FixShell: Sized { | ||
| type Handle; | ||
|
|
||
| fn create_blob(&self, data: &[u8]) -> Self::Handle; | ||
| fn create_tree(&self, data: &[Self::Handle]) -> Self::Handle; | ||
| fn create_ref(handle: Self::Handle) -> Self::Handle; | ||
|
|
||
| fn get_blob_data(&self, handle: Self::Handle) -> Box<[u8]>; | ||
| fn get_tree_data(&self, handle: Self::Handle) -> Box<[Self::Handle]>; | ||
|
|
||
| fn create_application_thunk(handle: Self::Handle) -> Self::Handle; | ||
| fn create_identification_thunk(handle: Self::Handle) -> Self::Handle; | ||
|
|
||
| fn create_strict_encode(handle: Self::Handle) -> Self::Handle; | ||
| } |
This file contains hidden or 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,27 @@ | ||
| ``` | ||
| A ::= O | K | E | L Any | ||
| | $atom Special/Primitive | ||
| | @path Executable path | ||
|
|
||
| B ::= int literal Blob | ||
| | string literal Blob | ||
|
|
||
| T ::= (A*) Tree | ||
|
|
||
| R ::= &B | &T Ref | ||
|
|
||
| O ::= B | T | R Data | ||
|
|
||
| K ::= ^O Thunk (Identify) | ||
| | *T Thunk (Apply) | ||
| | #T Thunk (Digest) | ||
| | ~T Thunk (Select) | ||
| | O[B] Thunk (Select 1) | ||
| | O[B:B] Thunk (Select N) | ||
|
|
||
| E ::= !K Encode (Strict) | ||
| | ?K Encode (Shallow) | ||
|
|
||
| L ::= (let ((name A)*) A) Let | ||
| ``` | ||
|
|
||
This file contains hidden or 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 @@ | ||
| extern crate alloc; | ||
| use alloc::collections::BTreeMap; | ||
|
|
||
| use crate::{FixShell, Storage, parser::Expr}; | ||
| use fixhandle::*; | ||
| use kernel::prelude::*; | ||
|
|
||
| pub struct Interpreter<'a> { | ||
| storage: &'a dyn Storage, | ||
| context: BTreeMap<String, Handle>, | ||
| } | ||
|
|
||
| impl<'a> Interpreter<'a> { | ||
| pub fn new(storage: &'a dyn Storage) -> Self { | ||
| Self { | ||
| storage, | ||
| context: BTreeMap::new(), | ||
| } | ||
| } | ||
|
|
||
| pub fn interpret(&mut self, expression: &Expr) -> Handle { | ||
| match expression { | ||
| Expr::String(str) => self.create_blob(str.as_bytes()), | ||
| Expr::Number(num) => self.create_blob(&i64::to_le_bytes(*num)), | ||
| Expr::Bytes(bytes) => self.create_blob(bytes), | ||
| Expr::Identifier(name) => *self.context.get(name).expect("undefined identifier"), | ||
| Expr::Ref(object) => Self::create_ref(self.interpret(object)), | ||
| Expr::Tree(handles) => { | ||
| let handles: Vec<Handle> = handles.iter().map(|x| self.interpret(x)).collect(); | ||
| self.create_tree(&handles) | ||
| } | ||
| Expr::Application(tree) => Self::create_application_thunk(self.interpret(tree)), | ||
| Expr::Identification(tree) => Self::create_identification_thunk(self.interpret(tree)), | ||
| Expr::StrictEncode(thunk) => Self::create_strict_encode(self.interpret(thunk)), | ||
| Expr::Let { bindings, body } => { | ||
| let outer_context = self.context.clone(); | ||
| for (name, expr) in bindings { | ||
| let handle = self.interpret(expr); | ||
| self.context.insert(name.clone(), handle); | ||
| } | ||
| let handle = self.interpret(body); | ||
| self.context = outer_context; | ||
| handle | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl FixShell for Interpreter<'_> { | ||
| type Handle = Handle; | ||
|
|
||
| fn create_blob(&self, data: &[u8]) -> Self::Handle { | ||
| self.storage.add_blob(data).into() | ||
| } | ||
|
|
||
| fn create_tree(&self, data: &[Self::Handle]) -> Self::Handle { | ||
| self.storage.add_tree(data).into() | ||
| } | ||
|
|
||
| fn create_ref(handle: Self::Handle) -> Self::Handle { | ||
| match handle { | ||
| Handle::Object(Object::Blob(blob)) => Handle::Ref(Ref::Blob(blob)), | ||
| Handle::Object(Object::Tree(tree)) => Handle::Ref(Ref::Tree(tree)), | ||
| _ => panic!("expected blob or tree handle"), | ||
| } | ||
| } | ||
|
|
||
| fn get_blob_data(&self, handle: Self::Handle) -> Box<[u8]> { | ||
| let Handle::Object(Object::Blob(blob)) = handle else { | ||
| panic!("expected blob handle") | ||
| }; | ||
| self.storage | ||
| .get_blob(blob) | ||
| .expect("blob data exists for handle") | ||
| } | ||
|
|
||
| fn get_tree_data(&self, handle: Self::Handle) -> Box<[Self::Handle]> { | ||
| let Handle::Object(Object::Tree(tree)) = handle else { | ||
| panic!("expected tree handle") | ||
| }; | ||
| self.storage | ||
| .get_tree(tree) | ||
| .expect("tree data exists for handle") | ||
| } | ||
|
|
||
| fn create_application_thunk(handle: Self::Handle) -> Self::Handle { | ||
| let Handle::Object(Object::Tree(tree)) = handle else { | ||
| panic!("expected tree handle for applicaiton") | ||
| }; | ||
| Thunk::Application(tree).into() | ||
| } | ||
|
|
||
| fn create_identification_thunk(handle: Self::Handle) -> Self::Handle { | ||
| Thunk::Identification(match handle { | ||
| Handle::Object(Object::Blob(blob)) => Ref::Blob(blob), | ||
| Handle::Object(Object::Tree(tree)) => Ref::Tree(tree), | ||
| Handle::Ref(reference) => reference, | ||
| _ => panic!("expected blob or tree handle"), | ||
| }) | ||
| .into() | ||
| } | ||
|
|
||
| fn create_strict_encode(handle: Self::Handle) -> Self::Handle { | ||
| let Handle::Thunk(thunk) = handle else { | ||
| panic!("expected thunk for strict encode") | ||
| }; | ||
| Encode::Strict(thunk).into() | ||
| } | ||
| } |
This file contains hidden or 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,5 @@ | ||
| mod fixshell; | ||
| mod interpreter; | ||
|
|
||
| pub use fixshell::*; | ||
| pub use interpreter::*; |
This file contains hidden or 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.
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.
It would be nice if this grammar were actually used directly by the code, rather than being a documentation file that will inevitably get out of date. Some Rust parsing crates support this, I'm not sure of any of them work in
no_stdbut it's possible. I know chumsky supportsno_stdbut it uses parser combinators instead of an external BNF grammar.Also I'm not sure handles should be a thing at this level? This seems more like you're using it to mean "any value", which is semantically different than a "handle"; Handles are a concrete 32-byte in-memory "pointer" in the implementation, while values are abstract objects in Fix. (although the meaning of "value" vs. "object" vs. "expression" vs. whatever else has changed a lot over time...)