Skip to content

Commit

Permalink
Lots of improvements to som-interpreter
Browse files Browse the repository at this point in the history
Added `Method` and `Primitive` class support
Removed `Block>>#whileTrue` as a primitive
Implemented `Block>>#restart`
Implemented more primitives
Improved how arguments are checked for correctness in primitives
Fixed class hierarchies and lookups
Blocks can now be evaluated outside of their original frame
Replaced most panics by in-interpreter exceptions
Improved string representations of values
Renamed `som_lexer::Symbol` to `som_lexer::Token`
Added more documentation
Completed README.md
Added `core-lib` as a submodule from `SOM-st/SOM`
  • Loading branch information
Hirevo committed Jun 5, 2020
1 parent 62e1f2e commit 8229c4a
Show file tree
Hide file tree
Showing 28 changed files with 1,373 additions and 639 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "core-lib"]
path = core-lib
url = https://github.com/SOM-st/SOM.git
82 changes: 41 additions & 41 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 60 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,63 @@ Here is a rundown of these different crates (as of now, layout may change in the
| **`som-parser-symbols`** | A SOM parser that works with **`som-lexer`**'s output. |

[**Cargo workspace**]: https://doc.rust-lang.org/cargo/reference/workspaces.html

How to build and run
--------------------

The interpreter is already usable, you can use it to start evaluating from a file or to have a Read-Eval-Print Loop to play around with the language.

To compile the program, you'll need to have Rust installed on your machine.
You can find the instructions on how to install on the [**official Rust language website**].
We recommend using whatever is the latest stable toolchain (which was 1.44 at the time of this writing).

[**official Rust language website**]: https://www.rust-lang.org/tools/install

Once you have Rust installed, simply run:

```bash
# the '--release' flag indicates to build with optimizations enabled.
# you can remove this flag if you wish to have more debug information in the emitted binary.
cargo build --release
```

This will compile the project and take care of fetching and building dependencies for you.
Once the build is finished, you should have a `target/` folder created in the current directory.
You'll find the interpreter's binary at `target/release/som-interpreter`.

To start the REPL, you can run:

```bash
# the '-c' flag instructs the interpreter where to find the SOM standard library.
./target/release/som-interpreter -c core-lib/Smalltalk
```

You'll get a prompt in which you can type SOM expressions and see what they get evaluated to.
The REPL makes the latest value successfully evaluated available via the `it` variable, so you can keep poking at the result of a previous expression, like this:

```plain
(0) SOM Shell | #(4 5 6)
returned: #(4 5 6) (Array([Integer(4), Integer(5), Integer(6)]))
(1) SOM Shell | it at: 2
returned: 5 (Integer(5))
(2) SOM Shell | it timesRepeat: [ 'hello from SOM !' println ]
hello from SOM !
hello from SOM !
hello from SOM !
hello from SOM !
hello from SOM !
returned: 5 (Integer(5))
```

To evaluate from a file, simply pass the file as another argument to the interpreter.
But, since the '-c' accepts multiple files, you might need to add the '--' argument before that file, like so:

```bash
./target/release/som-interpreter -c core-lib/Smalltalk -- core-lib/Examples/Hello.som
```

For other purposes, you can use '-h' (or '--help') to print the complete help message:

```bash
./target/release/som-interpreter -h
```
1 change: 1 addition & 0 deletions core-lib
Submodule core-lib added at 67a3f5
8 changes: 5 additions & 3 deletions som-interpreter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ path = "src/main.rs"
# internal
som-core = { path = "../som-core", version = "0.1.0" }
som-lexer = { path = "../som-lexer", version = "0.1.0" }
som-parser = { package = "som-parser-symbols", path = "../som-parser-symbols" }
# som-parser = { package = "som-parser-text", path = "../som-parser-text" }
som-parser = { package = "som-parser-symbols", path = "../som-parser-symbols", version = "0.1.0" }
# som-parser = { package = "som-parser-text", path = "../som-parser-text", version = "0.1.0" }

# CLI interface
structopt = "0.3.14"

# error handling
anyhow = "1.0.28"
anyhow = "1.0.31"

# big integers
num-bigint = "0.2.6"
15 changes: 12 additions & 3 deletions som-interpreter/src/block.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
use std::fmt;

use som_core::ast;

use crate::class::Class;
use crate::frame::Frame;
use crate::universe::Universe;
use crate::{SOMRef, SOMWeakRef};
use crate::SOMRef;

/// Represents an executable block.
#[derive(Debug, Clone)]
#[derive(Clone)]
pub struct Block {
/// Reference to the captured stack frame.
pub frame: SOMWeakRef<Frame>,
pub frame: SOMRef<Frame>,
/// Block definition from the AST.
pub block: ast::Block,
}
Expand All @@ -30,3 +32,10 @@ impl Block {
self.block.parameters.len()
}
}

impl fmt::Debug for Block {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct(&format!("Block{}", self.nb_parameters() + 1))
.finish()
}
}
Loading

0 comments on commit 8229c4a

Please sign in to comment.