Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
57 changes: 49 additions & 8 deletions Cargo.lock

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

13 changes: 12 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
[workspace]
resolver = "2"
members = ["crates/plotnik-cli", "crates/plotnik-lib", "crates/plotnik-langs", "crates/plotnik-core"]
members = [
"crates/plotnik-bytecode",
"crates/plotnik-cli",
"crates/plotnik-compiler",
"crates/plotnik-core",
"crates/plotnik-langs",
"crates/plotnik-lib",
"crates/plotnik-vm",
]

[workspace.package]
version = "0.2.2"
Expand All @@ -9,6 +17,9 @@ repository = "https://github.com/plotnik-lang/plotnik"
edition = "2024"

[workspace.dependencies]
plotnik-bytecode = { path = "crates/plotnik-bytecode", version = "0.2.2" }
plotnik-compiler = { path = "crates/plotnik-compiler", version = "0.2.2" }
plotnik-core = { path = "crates/plotnik-core", version = "0.2.2" }
plotnik-langs = { path = "crates/plotnik-langs", version = "0.2.2" }
plotnik-lib = { path = "crates/plotnik-lib", version = "0.2.2" }
plotnik-vm = { path = "crates/plotnik-vm", version = "0.2.2" }
18 changes: 18 additions & 0 deletions crates/plotnik-bytecode/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "plotnik-bytecode"
version.workspace = true
edition.workspace = true
license.workspace = true
repository.workspace = true
description = "Bytecode format and runtime types for Plotnik"

[lints.rust]
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(coverage_nightly)'] }

[dependencies]
plotnik-core.workspace = true
regex-automata = { version = "0.4", features = ["dfa-search"] }
thiserror = "2.0.17"

[dev-dependencies]
insta = { version = "=1.46.0", features = ["yaml"] }
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
use std::collections::BTreeMap;
use std::fmt::Write as _;

use crate::colors::Colors;

use crate::parser::PredicateOp;
use plotnik_core::Colors;
use crate::predicate_op::PredicateOp;

use super::format::{LineBuilder, Symbol, format_effect, nav_symbol, width_for_count};
use super::ids::TypeId;
use super::instructions::StepId;
use super::ir::NodeTypeIR;
use super::module::{Instruction, Module};
use super::node_type_ir::NodeTypeIR;
use super::nav::Nav;
use super::type_meta::{TypeData, TypeKind};
use super::{Call, Match, Return, Trampoline};
Expand Down Expand Up @@ -78,13 +77,13 @@ impl DumpContext {
let mut node_type_names = BTreeMap::new();
for i in 0..node_types.len() {
let t = node_types.get(i);
node_type_names.insert(t.id(), strings.get(t.name()).to_string());
node_type_names.insert(t.id, strings.get(t.name).to_string());
}

let mut node_field_names = BTreeMap::new();
for i in 0..node_fields.len() {
let f = node_fields.get(i);
node_field_names.insert(f.id(), strings.get(f.name()).to_string());
node_field_names.insert(f.id, strings.get(f.name).to_string());
}

// Collect all strings for unlinked mode lookups
Expand Down Expand Up @@ -210,14 +209,14 @@ fn dump_types_defs(out: &mut String, module: &Module, ctx: &DumpContext) {
TypeKind::Struct => {
let fields: Vec<_> = types
.members_of(&def)
.map(|m| strings.get(m.name()).to_string())
.map(|m| strings.get(m.name).to_string())
.collect();
format!("{} ; {{ {} }}{}", c.dim, fields.join(", "), c.reset)
}
TypeKind::Enum => {
let variants: Vec<_> = types
.members_of(&def)
.map(|m| strings.get(m.name()).to_string())
.map(|m| strings.get(m.name).to_string())
.collect();
format!("{} ; {}{}", c.dim, variants.join(" | "), c.reset)
}
Expand Down Expand Up @@ -293,8 +292,8 @@ fn format_type_name(type_id: TypeId, module: &Module, ctx: &DumpContext) -> Stri
// Try to find a name in types.names
for i in 0..types.names_count() {
let entry = types.get_name(i);
if entry.type_id() == type_id {
return strings.get(entry.name()).to_string();
if entry.type_id == type_id {
return strings.get(entry.name).to_string();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ impl EffectOpcode {

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct EffectOp {
pub(crate) opcode: EffectOpcode,
pub(crate) payload: usize,
pub opcode: EffectOpcode,
pub payload: usize,
}

impl EffectOp {
Expand All @@ -69,11 +69,4 @@ impl EffectOp {
let raw = ((self.opcode as u16) << 10) | ((self.payload as u16) & 0x3FF);
raw.to_le_bytes()
}

pub fn opcode(&self) -> EffectOpcode {
self.opcode
}
pub fn payload(&self) -> usize {
self.payload
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@ fn roundtrip_with_payload() {
let op = EffectOp::new(EffectOpcode::Set, 42);
let bytes = op.to_bytes();
let decoded = EffectOp::from_bytes(bytes);
assert_eq!(decoded.opcode(), EffectOpcode::Set);
assert_eq!(decoded.payload(), 42);
assert_eq!(decoded.opcode, EffectOpcode::Set);
assert_eq!(decoded.payload, 42);
}

#[test]
fn roundtrip_no_payload() {
let op = EffectOp::new(EffectOpcode::Node, 0);
let bytes = op.to_bytes();
let decoded = EffectOp::from_bytes(bytes);
assert_eq!(decoded.opcode(), EffectOpcode::Node);
assert_eq!(decoded.payload(), 0);
assert_eq!(decoded.opcode, EffectOpcode::Node);
assert_eq!(decoded.payload, 0);
}

#[test]
fn max_payload() {
let op = EffectOp::new(EffectOpcode::Enum, 1023);
let bytes = op.to_bytes();
let decoded = EffectOp::from_bytes(bytes);
assert_eq!(decoded.payload(), 1023);
assert_eq!(decoded.payload, 1023);
}

#[test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ use super::{StringId, TypeId};
#[repr(C)]
pub struct Entrypoint {
/// Definition name.
pub(crate) name: StringId,
pub name: StringId,
/// Starting instruction address.
pub(crate) target: StepAddr,
pub target: StepAddr,
/// Result type.
pub(crate) result_type: TypeId,
pub(crate) _pad: u16,
pub result_type: TypeId,
_pad: u16,
}

const _: () = assert!(std::mem::size_of::<Entrypoint>() == 8);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ pub struct Header {
pub entrypoints_count: u16,
pub transitions_count: u16,

// Bytes 44-63: Reserved
pub(crate) _reserved: [u8; 20],
// Bytes 44-63: Reserved (public for cross-crate struct initialization)
pub _reserved: [u8; 20],
}

const _: () = assert!(std::mem::size_of::<Header>() == 64);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use std::num::NonZeroU16;

use super::constants::{SECTION_ALIGN, STEP_SIZE};
use super::effects::EffectOp;
use super::ir::NodeTypeIR;
use super::nav::Nav;
use super::node_type_ir::NodeTypeIR;

/// Step address in bytecode (raw u16).
///
Expand Down Expand Up @@ -338,15 +338,15 @@ impl<'a> Match<'a> {
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct Call {
/// Segment index (0-3).
pub(crate) segment: u8,
pub segment: u8,
/// Navigation to apply before jumping to target.
pub(crate) nav: Nav,
pub nav: Nav,
/// Field constraint (None = no constraint).
pub(crate) node_field: Option<NonZeroU16>,
pub node_field: Option<NonZeroU16>,
/// Return address (current segment).
pub(crate) next: StepId,
pub next: StepId,
/// Callee entry point (target segment from type_id).
pub(crate) target: StepId,
pub target: StepId,
}

impl Call {
Expand Down Expand Up @@ -416,7 +416,7 @@ impl Call {
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct Return {
/// Segment index (0-3).
pub(crate) segment: u8,
pub segment: u8,
}

impl Return {
Expand Down Expand Up @@ -468,9 +468,9 @@ impl Default for Return {
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct Trampoline {
/// Segment index (0-3).
pub(crate) segment: u8,
pub segment: u8,
/// Return address (where to continue after entrypoint returns).
pub(crate) next: StepId,
pub next: StepId,
}

impl Trampoline {
Expand Down
Loading