summaryrefslogtreecommitdiff
path: root/oryxc/src/prelude.rs
diff options
context:
space:
mode:
Diffstat (limited to 'oryxc/src/prelude.rs')
-rw-r--r--oryxc/src/prelude.rs89
1 files changed, 70 insertions, 19 deletions
diff --git a/oryxc/src/prelude.rs b/oryxc/src/prelude.rs
index 5628c87..9ee42f1 100644
--- a/oryxc/src/prelude.rs
+++ b/oryxc/src/prelude.rs
@@ -1,29 +1,49 @@
-use std::collections::HashMap;
+use std::cell::UnsafeCell;
use std::fmt::{
self,
Debug,
Formatter,
};
+use std::sync::Arc;
+
+use dashmap::DashMap;
macro_rules! mkidtype {
($name:ident) => {
#[repr(transparent)]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct $name(pub u32);
+
impl $name {
#[allow(dead_code)]
pub const INVALID: Self = Self(u32::MAX);
}
+
impl From<usize> for $name {
fn from(n: usize) -> Self {
return Self(n as u32);
}
}
+
impl Into<usize> for $name {
fn into(self) -> usize {
return self.0 as usize;
}
}
+
+ impl<T> std::ops::Index<$name> for [T] {
+ type Output = T;
+
+ fn index(&self, index: $name) -> &Self::Output {
+ return &self[index.0 as usize];
+ }
+ }
+
+ impl<T> std::ops::IndexMut<$name> for [T] {
+ fn index_mut(&mut self, index: $name) -> &mut Self::Output {
+ return &mut self[index.0 as usize];
+ }
+ }
};
}
pub(crate) use mkidtype;
@@ -33,37 +53,50 @@ mkidtype!(ScopeId);
mkidtype!(SymbolId);
mkidtype!(TypeId);
-impl ScopeId {
- pub const GLOBAL: Self = Self(0);
-}
+/* Bypass Rust autism */
+#[repr(transparent)]
+#[derive(Debug)]
+pub struct TypeCell(UnsafeCell<TypeId>);
-impl TypeId {
- const MVMSK: u32 = 1 << 31;
+impl TypeCell {
+ pub fn new(id: TypeId) -> Self {
+ return Self(UnsafeCell::new(id));
+ }
- pub fn to_mvindex(&self) -> Option<usize> {
- return if self.0 & Self::MVMSK != 0 {
- Some((self.0 & !Self::MVMSK) as usize)
- } else {
- None
- };
+ pub fn get(&self) -> TypeId {
+ return unsafe { *self.0.get() };
}
- pub fn from_mvindex(i: usize) -> Self {
- return Self(i as u32 & Self::MVMSK);
+ pub fn set(&self, id: TypeId) {
+ return unsafe { *self.0.get() = id };
}
}
+unsafe impl Sync for TypeCell {}
+
+impl ScopeId {
+ pub const GLOBAL: Self = Self(0);
+}
+
+/// A fully-qualified symbol name
+#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
+pub struct QualifiedId {
+ pub file: FileId,
+ pub scope: ScopeId,
+ pub sym: SymbolId,
+}
+
#[derive(Debug)]
pub struct Scope {
pub parent: ScopeId,
- pub symtab: HashMap<SymbolId, Symbol>,
+ pub symtab: DashMap<SymbolId, Symbol>,
}
impl Scope {
pub fn new(parent: ScopeId) -> Self {
return Self {
parent,
- symtab: HashMap::new(),
+ symtab: DashMap::new(),
};
}
}
@@ -82,6 +115,17 @@ pub enum ResolutionState {
pub struct Symbol {
pub state: ResolutionState,
pub kind: SymbolType,
+ pub vtype: TypeId,
+}
+
+impl Symbol {
+ pub fn new(kind: SymbolType) -> Self {
+ return Self {
+ state: ResolutionState::Unresolved,
+ kind,
+ vtype: TypeId::INVALID,
+ };
+ }
}
#[repr(u8)]
@@ -91,20 +135,27 @@ pub enum SymbolType {
FuncParam,
}
+#[derive(Clone, Eq, PartialEq, Hash)]
pub enum OryxType {
+ /* Untyped types – the types of constant literals */
+ UBoolean,
+ UNumber,
+ UString,
+
Boolean,
Function {
- args: Box<[TypeId]>,
- rets: Box<[TypeId]>,
+ args: Arc<[TypeId]>,
+ rets: Arc<[TypeId]>,
},
Integer {
bits: usize,
signed: bool,
},
Pointer {
- base: u32,
+ base: TypeId,
},
Type(TypeId),
+ MultiValue(Arc<[TypeId]>),
}
#[derive(Clone, Copy)]