Alpha Chen 2 years ago
parent 280a64d02c
commit 7073e57123
Signed by: alpha
SSH Key Fingerprint: SHA256:3fOT8fiYQG/aK9ntivV3Bqtg8AYQ7q4nV6ZgihOA20g

@ -1,6 +1,7 @@
# Crafting Interpreters
- [Crafting Interpreters](https://craftinginterpreters.com/)
- [Source repo](https://github.com/munificent/craftinginterpreters)
## Installing Lox

65
rust/Cargo.lock generated

@ -2,6 +2,71 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "anyhow"
version = "1.0.62"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1485d4d2cc45e7b201ee3767015c96faa5904387c9d87c6efdd0fb511f12d305"
[[package]]
name = "lox"
version = "0.1.0"
dependencies = [
"anyhow",
"thiserror",
]
[[package]]
name = "proc-macro2"
version = "1.0.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0a2ca2c61bc9f3d74d2886294ab7b9853abd9c1ad903a3ac7815c58989bb7bab"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179"
dependencies = [
"proc-macro2",
]
[[package]]
name = "syn"
version = "1.0.99"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "58dbef6ec655055e20b86b15a8cc6d439cca19b667537ac6a1369572d151ab13"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "thiserror"
version = "1.0.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f5f6586b7f764adc0231f4c79be7b920e766bb2f3e51b3661cdb263828f19994"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "1.0.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "12bafc5b54507e0149cdf1b145a5d80ab80a90bcd9275df43d4fff68460f6c21"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "unicode-ident"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4f5b37a154999a8f3f98cc23a628d850e154479cd94decf3414696e12e31aaf"

@ -6,3 +6,5 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
anyhow = "1.0.62"
thiserror = "1.0.32"

@ -15,7 +15,7 @@ type Value = f32;
#[derive(Default)]
pub struct Chunk {
code: Vec<u8>,
pub code: Vec<u8>,
constants: Vec<Value>,
lines: Lines,
}
@ -74,7 +74,7 @@ impl Chunk {
}
fn constant_instruction(&self, name: &str, offset: usize) -> usize {
let constant_index = self.code[offset+1];
let constant_index = self.code[offset + 1];
let value = self.constants[constant_index as usize];
println!("{:<16} {:>4} '{}'", name, constant_index, value);
offset + 2
@ -82,7 +82,7 @@ impl Chunk {
fn constant_long_instruction(&self, name: &str, offset: usize) -> usize {
let index_len = mem::size_of::<usize>();
let index_bytes = &self.code[offset+1..offset+1+index_len];
let index_bytes = &self.code[offset + 1..offset + 1 + index_len];
let (int_bytes, _) = index_bytes.split_at(std::mem::size_of::<usize>());
let constant_index = usize::from_ne_bytes(int_bytes.try_into().unwrap());
@ -108,7 +108,7 @@ fn test_constant_long() {
// Lines are stored using run-length encoding, where the first element is the line and the second
// element the number of instructions that are associated with that line
#[derive(Default, Debug)]
#[derive(Debug, Default)]
struct Lines(std::vec::Vec<(usize, usize)>);
impl Lines {
@ -146,4 +146,3 @@ fn test_get_line() {
assert_eq!(lines.get(2), (2, true));
assert_eq!(lines.get(3), (2, false));
}

@ -1,7 +1,9 @@
use chunk::{Chunk, OpCode};
use vm::VM;
pub mod chunk;
pub mod vec;
pub mod vm;
fn main() {
let mut chunk = Chunk::default();
@ -16,5 +18,8 @@ fn main() {
// chunk.write_constant(0.0, 123);
chunk.disassemble("test chunk");
// chunk.disassemble("test chunk");
let vm = VM::new(chunk);
vm.interpret();
}

@ -0,0 +1,33 @@
use std::ptr;
use anyhow::Result;
use thiserror::Error;
use crate::chunk::Chunk;
pub struct VM {
chunk: Chunk,
ip: *const u8,
}
#[derive(Error, Debug)]
pub enum DataStoreError {
#[error("")]
CompileError,
#[error("")]
RuntimeError,
}
impl VM {
pub fn new(chunk: Chunk) -> Self {
VM { chunk, ip: &chunk.code.ptr }
}
pub fn interpret(&self) -> Result<()> {
self.run()
}
fn run(&self) -> Result<()> {
todo!()
}
}
Loading…
Cancel
Save