[rlox] init
This commit is contained in:
2
rlox/.gitignore
vendored
Normal file
2
rlox/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
target/
|
||||
.projectile
|
||||
7
rlox/Cargo.lock
generated
Normal file
7
rlox/Cargo.lock
generated
Normal 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
8
rlox/Cargo.toml
Normal 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
5
rlox/src/bytecode.rs
Normal file
@@ -0,0 +1,5 @@
|
||||
#[repr(u8)]
|
||||
#[derive(Debug)]
|
||||
pub enum Op {
|
||||
Return
|
||||
}
|
||||
32
rlox/src/chunk.rs
Normal file
32
rlox/src/chunk.rs
Normal 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
11
rlox/src/main.rs
Normal 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);
|
||||
}
|
||||
Reference in New Issue
Block a user