[rlox] init

This commit is contained in:
ctsk
2023-03-29 20:03:16 +02:00
parent 26cbca5c55
commit ff67071f5b
6 changed files with 65 additions and 0 deletions

2
rlox/.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
target/
.projectile

7
rlox/Cargo.lock generated Normal file
View File

@@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "rlox"
version = "0.1.0"

8
rlox/Cargo.toml Normal file
View File

@@ -0,0 +1,8 @@
[package]
name = "rlox"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

5
rlox/src/bytecode.rs Normal file
View File

@@ -0,0 +1,5 @@
#[repr(u8)]
#[derive(Debug)]
pub enum Op {
Return
}

32
rlox/src/chunk.rs Normal file
View File

@@ -0,0 +1,32 @@
use crate::Op;
use std::fmt;
pub struct Chunk {
code: Vec<Op>,
name: String
}
impl Chunk {
pub fn new(name: String) -> Chunk {
Chunk {
code: Vec::new(),
name
}
}
pub fn add(&mut self, op: Op) {
self.code.push(op)
}
}
impl fmt::Debug for Chunk {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
writeln!(f, "-*-*- {} -*-*-", self.name)?;
for (idx, op) in self.code.iter().enumerate() {
write!(f, "{:04} {:?}", idx, op)?;
}
return Ok(());
}
}

11
rlox/src/main.rs Normal file
View File

@@ -0,0 +1,11 @@
mod chunk;
mod bytecode;
use chunk::Chunk;
use bytecode::Op;
fn main() {
let mut chunk = Chunk::new("TEST".to_string());
chunk.add(Op::Return);
println!("{:?}", chunk);
}