diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index ed11610..7ba4438 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -7,23 +7,20 @@
-
+
+
+
-
-
-
-
-
-
-
-
-
+
+
+
+
-
-
-
+
+
+
@@ -92,6 +89,7 @@
}
+
@@ -158,7 +156,7 @@
-
+
@@ -288,7 +286,15 @@
1717614243407
-
+
+
+ 1717614696137
+
+
+
+ 1717614696137
+
+
@@ -303,6 +309,7 @@
-
+
+
\ No newline at end of file
diff --git a/src/root/builtin/int/add.rs b/src/root/builtin/int/add.rs
new file mode 100644
index 0000000..1cb767f
--- /dev/null
+++ b/src/root/builtin/int/add.rs
@@ -0,0 +1,39 @@
+use unique_type_id::UniqueTypeId;
+use crate::root::builtin::{BuiltinInlineFunction, InlineFunctionGenerator};
+use crate::root::builtin::int::IntType;
+use crate::root::errors::WErr;
+use crate::root::name_resolver::name_resolvers::NameResult::Function;
+use crate::root::name_resolver::resolve_function_signatures::FunctionSignature;
+
+use crate::root::shared::common::{FunctionID, Indirection, LocalAddress, TypeID, TypeRef};
+
+#[derive(UniqueTypeId)]
+#[UniqueTypeIdType = "u16"]
+pub struct IntAdd {}
+
+impl BuiltinInlineFunction for IntAdd {
+ fn id(&self) -> FunctionID {
+ FunctionID(-(IntAdd::unique_type_id().0 as isize) - 1)
+ }
+
+ fn name(&self) -> &'static str {
+ "add"
+ }
+
+ fn signature(&self) -> FunctionSignature {
+ FunctionSignature::new_inline_builtin(
+ &[("lhs", IntType::id().immediate()), ("rhs", IntType::id().immediate())],
+ Some(IntType::id().immediate())
+ )
+ }
+
+ fn inline(&self) -> InlineFunctionGenerator {
+ |args: &[LocalAddress], return_addr: Option| -> Result {
+ todo!()
+ }
+ }
+
+ fn parent_type(&self) -> Option {
+ Some(IntType::id())
+ }
+}
diff --git a/src/root/builtin/int.rs b/src/root/builtin/int/mod.rs
similarity index 79%
rename from src/root/builtin/int.rs
rename to src/root/builtin/int/mod.rs
index c80b638..27d0a64 100644
--- a/src/root/builtin/int.rs
+++ b/src/root/builtin/int/mod.rs
@@ -1,10 +1,20 @@
+mod add;
+
+use b_box::b;
use unique_type_id::UniqueTypeId;
+use crate::root::builtin::int::add::IntAdd;
use crate::root::compiler::assembly::utils::get_qword_stack_pointer;
use crate::root::errors::WErr;
+use crate::root::name_resolver::name_resolvers::GlobalDefinitionTable;
use crate::root::parser::parse_function::parse_literal::{LiteralToken, LiteralTokens};
use crate::root::shared::common::{AddressedTypeRef, ByteSize, FunctionID, LocalAddress, TypeID};
use crate::root::shared::types::Type;
+pub fn register_int(global_table: &mut GlobalDefinitionTable) {
+ global_table.register_builtin_type("int".to_string(), b!(IntType{}));
+ global_table.register_inline_function(&IntAdd{});
+}
+
#[derive(UniqueTypeId)]
#[UniqueTypeIdType = "u16"]
pub struct IntType {}
diff --git a/src/root/builtin/mod.rs b/src/root/builtin/mod.rs
index c88c275..419d697 100644
--- a/src/root/builtin/mod.rs
+++ b/src/root/builtin/mod.rs
@@ -1,15 +1,22 @@
pub mod int;
-use crate::root::builtin::int::IntType;
+use crate::root::builtin::int::{IntType, register_int};
+use crate::root::errors::WErr;
use crate::root::name_resolver::name_resolvers::{GlobalDefinitionTable};
+use crate::root::name_resolver::resolve_function_signatures::FunctionSignature;
+use crate::root::shared::common::{FunctionID, LocalAddress, TypeID};
use crate::root::shared::types::Type;
pub fn register_builtin(global_table: &mut GlobalDefinitionTable) {
- let types: [(String, Box); 1] = [
- ("int".to_string(), Box::new(IntType{}))
- ];
-
- for (n, t) in types {
- global_table.register_builtin_type(n, t);
- }
+ register_int(global_table);
}
+
+pub type InlineFunctionGenerator = fn(&[LocalAddress], Option) -> Result;
+
+pub trait BuiltinInlineFunction {
+ fn id(&self) -> FunctionID;
+ fn name(&self) -> &'static str;
+ fn signature(&self) -> FunctionSignature;
+ fn inline(&self) -> InlineFunctionGenerator;
+ fn parent_type(&self) -> Option;
+}
\ No newline at end of file
diff --git a/src/root/compiler/compile_function_call.rs b/src/root/compiler/compile_function_call.rs
new file mode 100644
index 0000000..1094cc4
--- /dev/null
+++ b/src/root/compiler/compile_function_call.rs
@@ -0,0 +1,6 @@
+use crate::root::errors::WErr;
+use crate::root::shared::common::{FunctionID, LocalAddress};
+
+pub fn call_function(fid: FunctionID, arguments: &[LocalAddress], return_address: Option) -> Result {
+ todo!()
+}
\ No newline at end of file
diff --git a/src/root/compiler/mod.rs b/src/root/compiler/mod.rs
index f98fc7e..f56240c 100644
--- a/src/root/compiler/mod.rs
+++ b/src/root/compiler/mod.rs
@@ -2,4 +2,5 @@ pub mod compile;
pub mod local_variable_table;
mod compile_function;
pub mod assembly;
-mod compile_evaluable;
\ No newline at end of file
+mod compile_evaluable;
+pub mod compile_function_call;
\ No newline at end of file
diff --git a/src/root/name_resolver/mod.rs b/src/root/name_resolver/mod.rs
index 5159ccc..bcd97ee 100644
--- a/src/root/name_resolver/mod.rs
+++ b/src/root/name_resolver/mod.rs
@@ -2,4 +2,4 @@ pub mod resolve;
pub mod resolve_names;
pub mod resolve_type_sizes;
pub mod name_resolvers;
-mod resolve_function_signatures;
\ No newline at end of file
+pub mod resolve_function_signatures;
\ No newline at end of file
diff --git a/src/root/name_resolver/name_resolvers.rs b/src/root/name_resolver/name_resolvers.rs
index be1926c..68e9701 100644
--- a/src/root/name_resolver/name_resolvers.rs
+++ b/src/root/name_resolver/name_resolvers.rs
@@ -4,6 +4,8 @@ use std::path::PathBuf;
use std::rc::Rc;
use derive_getters::Getters;
use either::{Either, Left, Right};
+use crate::root::builtin::{BuiltinInlineFunction, InlineFunctionGenerator};
+use crate::root::compiler::compile_function_call::call_function;
use crate::root::compiler::local_variable_table::LocalVariableTable;
use crate::root::errors::name_resolver_errors::NRErrors;
use crate::root::errors::name_resolver_errors::NRErrors::IdentifierNotFound;
@@ -16,7 +18,7 @@ use crate::root::parser::parse_function::parse_evaluable::{FullNameToken, FullNa
use crate::root::parser::parse_name::SimpleNameToken;
use crate::root::parser::parse_struct::StructToken;
use crate::root::POINTER_SIZE;
-use crate::root::shared::common::{AddressedTypeRef, ByteSize, FunctionID, TypeID, TypeRef};
+use crate::root::shared::common::{AddressedTypeRef, ByteSize, FunctionID, LocalAddress, TypeID, TypeRef};
#[derive(Debug)]
enum NameTreeEntry {
@@ -55,11 +57,13 @@ impl TopLevelNameTree {
}
}
+
pub struct GlobalDefinitionTable {
id_counter: isize,
type_definitions: HashMap>,
impl_definitions: HashMap>,
function_signatures: HashMap,
+ inline_functions: HashMap,
name_table: TopLevelNameTree,
builtin_type_name_table: HashMap,
builtin_function_name_table: HashMap
@@ -79,6 +83,7 @@ impl GlobalDefinitionTable {
type_definitions: Default::default(),
impl_definitions: Default::default(),
function_signatures: Default::default(),
+ inline_functions: Default::default(),
name_table: Default::default(),
builtin_type_name_table: Default::default(),
builtin_function_name_table: Default::default(),
@@ -90,9 +95,29 @@ impl GlobalDefinitionTable {
self.builtin_type_name_table.insert(name, id);
}
- pub fn register_builtin_function(&mut self, name: String, t: FunctionSignature, id: FunctionID) {
- self.function_signatures.insert(id, t);
- self.builtin_function_name_table.insert(name, id);
+ // pub fn register_builtin_function(&mut self, name: String, t: FunctionSignature, id: FunctionID) {
+ // self.function_signatures.insert(id, t);
+ // self.builtin_function_name_table.insert(name, id);
+ // }
+
+ fn get_impl_mut(&mut self, t: TypeID) -> &mut HashMap {
+ if !self.impl_definitions.contains_key(&t) {
+ self.impl_definitions.insert(t, Default::default());
+ }
+
+ self.impl_definitions.get_mut(&t).unwrap()
+ }
+
+ pub fn register_inline_function(&mut self, inline: &dyn BuiltinInlineFunction) {
+ self.function_signatures.insert(inline.id(), inline.signature());
+ self.inline_functions.insert(inline.id(), inline.inline());
+
+ if let Some(parent) = inline.parent_type() {
+ self.get_impl_mut(parent).insert(inline.name().to_string(), inline.id());
+ }
+ else {
+ self.builtin_function_name_table.insert(inline.name().to_string(), inline.id());
+ }
}
pub fn add_from_struct_token(&mut self, st: &StructToken) -> TypeID {
@@ -271,4 +296,12 @@ impl GlobalDefinitionTable {
Err(WErr::n(NRErrors::CannotFindName(name.name().clone()), name.location().clone()))
}
+
+ pub fn call_function(&self, function: FunctionID, arguments: &[LocalAddress], return_address: Option) -> Result {
+ if let Some(inline) = self.inline_functions.get(&function) {
+ return inline(arguments, return_address);
+ }
+
+ call_function(function, arguments, return_address)
+ }
}
diff --git a/src/root/name_resolver/resolve_function_signatures.rs b/src/root/name_resolver/resolve_function_signatures.rs
index 72eb31e..d71c5a9 100644
--- a/src/root/name_resolver/resolve_function_signatures.rs
+++ b/src/root/name_resolver/resolve_function_signatures.rs
@@ -1,4 +1,5 @@
use derive_getters::Getters;
+use itertools::Itertools;
use crate::root::errors::WErr;
use crate::root::name_resolver::name_resolvers::{GlobalDefinitionTable};
use crate::root::shared::common::TypeRef;
@@ -11,6 +12,15 @@ pub struct FunctionSignature {
return_type: Option
}
+impl FunctionSignature {
+ pub fn new_inline_builtin(args: &[(&str, TypeRef)], return_type: Option) -> FunctionSignature {
+ FunctionSignature {
+ args: args.into_iter().map(|(name, t)| (SimpleNameToken::new_builtin(name.to_string()), t.clone())).collect_vec(),
+ return_type
+ }
+ }
+}
+
pub fn resolve_function_signature(function_token: &FunctionToken, global_table: &mut GlobalDefinitionTable) -> Result {
let mut args = Vec::new();
diff --git a/src/root/parser/parse.rs b/src/root/parser/parse.rs
index a44cf1b..909f336 100644
--- a/src/root/parser/parse.rs
+++ b/src/root/parser/parse.rs
@@ -1,4 +1,5 @@
use std::cmp::min;
+use std::ffi::OsStr;
use std::fmt::{Display, Formatter};
use crate::root::parser::parse_toplevel;
use nom::IResult;
@@ -9,6 +10,7 @@ use std::path::PathBuf;
use std::rc::Rc;
use color_print::cformat;
use derive_getters::Getters;
+use lazy_static::lazy_static;
use crate::root::errors::WErr;
use crate::root::parser::parse_toplevel::TopLevelTokens;
@@ -22,6 +24,10 @@ pub type ErrorTree<'a> = GenericErrorTree<
Box,
>;
+lazy_static! {
+ static ref BUILTIN_PATH: &'static OsStr = OsStr::new("builtin");
+}
+
#[derive(Debug, Clone, Getters, Hash)]
pub struct Location {
path: Rc,
@@ -39,6 +45,18 @@ impl Location {
line: span.location_line(),
}
}
+
+ pub fn builtin() -> Location {
+ Location {
+ path: Rc::new(PathBuf::from(*BUILTIN_PATH)),
+ offset: 0,
+ line: 0
+ }
+ }
+
+ pub fn is_builtin(&self) -> bool {
+ self.path.as_os_str() == *BUILTIN_PATH
+ }
}
const CHAR_LIMIT: usize = 61;
@@ -49,6 +67,11 @@ impl Display for Location {
// TODO: Inefficient!
// (Maybe fine because it is a 'bad' path?)
+ if self.path.as_os_str() == *BUILTIN_PATH {
+ writeln!(f, "{}", cformat!("Builtin Definition>"))?;
+ return Ok(())
+ }
+
writeln!(f, "{}", cformat!("In File:>"))?;
writeln!(f, " {}", self.path.as_path().to_string_lossy())?;
writeln!(f, "{}", cformat!("At:>"))?;
diff --git a/src/root/parser/parse_name.rs b/src/root/parser/parse_name.rs
index ae3bee2..ee1f174 100644
--- a/src/root/parser/parse_name.rs
+++ b/src/root/parser/parse_name.rs
@@ -17,6 +17,13 @@ impl SimpleNameToken {
name: s.to_string()
}
}
+
+ pub fn new_builtin(s: String) -> SimpleNameToken {
+ SimpleNameToken {
+ location: Location::builtin(),
+ name: s.to_string()
+ }
+ }
}
pub fn parse_simple_name<'a>(s: Span<'a>) -> ParseResult<'a, Span, SimpleNameToken> {
diff --git a/src/root/shared/common.rs b/src/root/shared/common.rs
index e8370a3..c731450 100644
--- a/src/root/shared/common.rs
+++ b/src/root/shared/common.rs
@@ -5,6 +5,16 @@ use derive_getters::{Dissolve, Getters};
#[display(fmt = "TypeID: {}", .0)]
pub struct TypeID(pub isize);
+impl TypeID {
+ pub fn with_indirection(self, indirection: usize) -> TypeRef {
+ TypeRef::new(self, Indirection(indirection))
+ }
+
+ pub fn immediate(self) -> TypeRef {
+ TypeRef::new(self, Indirection(0))
+ }
+}
+
#[derive(Debug, PartialEq, Eq, Hash, Display, Copy, Clone)]
#[display(fmt = "FunctionID: {}", .0)]
pub struct FunctionID(pub isize);
diff --git a/types.toml b/types.toml
index 3d57cf4..977876b 100644
--- a/types.toml
+++ b/types.toml
@@ -1 +1,2 @@
IntType=0
+IntAdd=1