Skip to content

Commit

Permalink
feat: Add uuid for functions
Browse files Browse the repository at this point in the history
  • Loading branch information
sbwtw committed Jan 6, 2024
1 parent 6b3a1b2 commit ea6827f
Show file tree
Hide file tree
Showing 17 changed files with 218 additions and 8 deletions.
60 changes: 60 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ smallvec = { version = "*", features = ["union", "const_generics", "const_new"]
smallmap = "*"
log = "*"
indexmap = "*"
uuid = { version = "*", features = ["v4", "fast-rng", "macro-diagnostics", "serde"] }

[build-dependencies]
cbindgen = "*"
Expand Down
14 changes: 14 additions & 0 deletions lib/src/ast/declaration_statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ pub struct Declaration {
pub kind: DeclKind,
}

impl HasAttribute for Declaration {
fn set_attribute<K: AsRef<StString>, V: Into<String>>(&mut self, k: K, v: V) {
todo!()
}

fn get_attribute_value<S: AsRef<StString>>(&self, attr: &S) -> Option<&String> {
todo!()
}

fn remove_attribute<K: AsRef<StString>>(&mut self, k: K) -> Option<String> {
todo!()
}
}

impl Declaration {
pub fn identifier(&self) -> &StString {
match self.kind {
Expand Down
1 change: 0 additions & 1 deletion lib/src/ast/function_declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use std::rc::Rc;
#[derive(Debug)]
pub struct FunctionDeclare {
name: StString,
#[allow(dead_code)]
decl_class: DeclareClass,
return_type: Option<Rc<Box<dyn Type>>>,
parameters: SmallVec<[Rc<Variable>; 8]>,
Expand Down
22 changes: 22 additions & 0 deletions lib/src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,28 @@ impl Display for dyn Type {
}
}

bitflags! {
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct CompilePassFlags: u32 {
const NONE = 0b0000_0000_0000_0000;

// HasError
const HAS_ERROR = 0b0001_0000_0000_0001;
// TypeChecked
const TYPE_CHECKED = 0b0000_0000_0000_0010;
}
}

bitflags! {
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct FunctionFlags: u32 {
const NONE = 0b0000_0000_0000_0000;

// This Function generated by compiler
const COMPILER_GENERATED = 0b0000_0000_0000_0001;
}
}

bitflags! {
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct VariableFlags: u32 {
Expand Down
5 changes: 5 additions & 0 deletions lib/src/backend/lua/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ pub enum LuaByteCode {
LoadI(u8, u32),
/// A B C: R[A] := R[B] + R[C]
Add(u8, u8, u8),
/// A sB k: if ((R[A] >= sB) ~= k) then pc++
Gei(u8, u8, u8),
}

impl LuaByteCode {
Expand All @@ -69,6 +71,7 @@ impl LuaByteCode {
LuaByteCode::Move(..) => "MOVE",
LuaByteCode::LoadI(..) => "LOADI",
LuaByteCode::Add(..) => "ADD",
LuaByteCode::Gei(..) => "GEI",
}
}

Expand All @@ -81,6 +84,7 @@ impl LuaByteCode {
LuaByteCode::Move(..) => LuaOpCode::OP_MOVE,
LuaByteCode::LoadI(..) => LuaOpCode::OP_LOADI,
LuaByteCode::Add(..) => LuaOpCode::OP_ADD,
LuaByteCode::Gei(..) => LuaOpCode::OP_GEI,
}
}
}
Expand All @@ -101,6 +105,7 @@ impl LuaCode {
LuaByteCode::Call(a, b, c)
| LuaByteCode::GetTabUp(a, b, c)
| LuaByteCode::SetTabUp(a, b, c)
| LuaByteCode::Gei(a, b, c)
| LuaByteCode::Add(a, b, c) => {
write!(s, "{a} {b} {c}").unwrap();
}
Expand Down
1 change: 1 addition & 0 deletions lib/src/backend/lua/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use bytecode::*;
mod encoding;
mod register;
mod utils;
mod vm;

use crate::backend::lua::register::{RegisterId, RegisterManager};
use crate::backend::lua::utils::try_fit_sbx;
Expand Down
1 change: 1 addition & 0 deletions lib/src/backend/lua/vm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub struct LuaVM {}
2 changes: 2 additions & 0 deletions lib/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ mod units_manager;
pub use units_manager::UnitsManager;

mod scope;
mod task;

pub use scope::Scope;

pub enum ModuleContextScope {
Expand Down
72 changes: 69 additions & 3 deletions lib/src/context/module_context.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use crate::ast::*;
use crate::backend::TargetCode;
use crate::context::task::TaskInfo;
use crate::context::ModuleContextScope;
use crate::parser::StString;
use indexmap::IndexMap;
use log::warn;
use once_cell::sync::Lazy;
use smallvec::smallvec;
use std::borrow::Cow;
use std::collections::{HashMap, HashSet};
use std::fmt::{Display, Formatter};
Expand All @@ -12,6 +15,7 @@ use std::ops::Deref;
use std::rc::Rc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard};
use uuid::Uuid;

static CONTEXT_ID: Lazy<AtomicUsize> = Lazy::new(|| AtomicUsize::new(0));
static DECLARATION_ID: Lazy<AtomicUsize> = Lazy::new(|| AtomicUsize::new(0));
Expand Down Expand Up @@ -57,6 +61,12 @@ impl Prototype {
inner: Rc::new(RwLock::new(PrototypeImpl::new(decl))),
}
}

fn with_object_id(decl: Declaration, id: Uuid) -> Self {
Self {
inner: Rc::new(RwLock::new(PrototypeImpl::with_object_id(decl, id))),
}
}
}

#[derive(Clone)]
Expand All @@ -79,6 +89,12 @@ impl Function {
}
}

fn with_prototype(proto: &Prototype, function: Statement) -> Self {
Self {
inner: Rc::new(RwLock::new(FunctionImpl::with_prototype(proto, function))),
}
}

pub fn read(&self) -> RwLockReadGuard<'_, FunctionImpl> {
self.inner.read().unwrap()
}
Expand All @@ -90,17 +106,31 @@ impl Function {

pub struct PrototypeImpl {
id: usize,
object_id: Uuid,
decl: Declaration,
}

impl PrototypeImpl {
fn new(decl: Declaration) -> Self {
Self {
id: get_next_declaration_id(),
object_id: Uuid::nil(),
decl,
}
}

fn with_object_id(decl: Declaration, id: Uuid) -> Self {
Self {
id: get_next_declaration_id(),
object_id: id,
decl,
}
}

pub fn object_id(&self) -> Uuid {
self.object_id
}

pub fn decl(&self) -> &Declaration {
&self.decl
}
Expand Down Expand Up @@ -194,6 +224,7 @@ impl Display for PrototypeImpl {

pub struct FunctionImpl {
decl_id: usize,
object_id: Uuid,
parse_tree: Statement,
compiled_code: Option<Box<dyn TargetCode>>,
}
Expand All @@ -202,11 +233,27 @@ impl FunctionImpl {
fn new(decl_id: usize, function: Statement) -> Self {
Self {
decl_id,
object_id: Uuid::nil(),
parse_tree: function,
compiled_code: None,
}
}

fn with_prototype(proto: &Prototype, function: Statement) -> Self {
let proto = proto.read().unwrap();

Self {
decl_id: proto.id,
object_id: proto.object_id(),
parse_tree: function,
compiled_code: None,
}
}

pub fn object_id(&self) -> Uuid {
self.object_id
}

pub fn decl_id(&self) -> usize {
self.decl_id
}
Expand Down Expand Up @@ -251,6 +298,7 @@ impl ModuleContext {
declaration_name_map: HashMap::new(),
function_id_map: IndexMap::new(),
toplevel_global_variable_declarations: HashSet::new(),
task_info: smallvec![],
})),
}
}
Expand All @@ -271,6 +319,7 @@ pub struct ModuleContextImpl {
declaration_name_map: HashMap<StString, Prototype>,
function_id_map: IndexMap<usize, Function>,
toplevel_global_variable_declarations: HashSet<Prototype>,
task_info: SmallVec8<TaskInfo>,
}

impl ModuleContextImpl {
Expand All @@ -282,7 +331,7 @@ impl ModuleContextImpl {
&self.scope
}

pub fn add_declaration(&mut self, decl: Declaration) -> usize {
pub fn add_declaration(&mut self, decl: Declaration, id: Uuid) -> usize {
let name = decl.identifier().clone();
let mut toplevel_global_variable_declaration = false;

Expand All @@ -292,7 +341,7 @@ impl ModuleContextImpl {
}
}

let decl = Prototype::new(decl);
let decl = Prototype::with_object_id(decl, id);
let proto_id = decl.read().unwrap().id;

self.declaration_id_map.insert(proto_id, decl.clone());
Expand All @@ -306,7 +355,24 @@ impl ModuleContextImpl {
}

pub fn add_function(&mut self, decl_id: usize, fun: Statement) -> Option<Function> {
let fun = Function::new(decl_id, fun);
let fun = match self.get_declaration_by_id(decl_id) {
Some(decl) => Function::with_prototype(decl, fun),
_ => {
warn!("Declaration of function {decl_id} not found");
Function::new(decl_id, fun)
}
};

self.function_id_map.insert(decl_id, fun)
}

pub fn add_function_with_proto(
&mut self,
proto: &Prototype,
fun: Statement,
) -> Option<Function> {
let decl_id = proto.read().unwrap().id;
let fun = Function::with_prototype(proto, fun);

self.function_id_map.insert(decl_id, fun)
}
Expand Down
5 changes: 5 additions & 0 deletions lib/src/context/task.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use uuid::Uuid;

pub(crate) struct TaskInfo {
pub entry_object_id: Uuid,
}
2 changes: 2 additions & 0 deletions lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ pub mod prelude {
pub use crate::ast::*;
pub use crate::context::*;
pub use crate::parser::StString;

pub use uuid::Uuid;
}

#[cfg(test)]
Expand Down
Loading

0 comments on commit ea6827f

Please sign in to comment.