diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index bbf98a2..774f412 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -8,7 +8,41 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -75,6 +109,11 @@
"vue.rearranger.settings.migration": "true"
}
}
+
+
+
+
+
@@ -131,6 +170,7 @@
+
diff --git a/build/a.out b/build/a.out
deleted file mode 100755
index e28dffb..0000000
Binary files a/build/a.out and /dev/null differ
diff --git a/build/out.asm b/build/out.asm
deleted file mode 100644
index 696a415..0000000
--- a/build/out.asm
+++ /dev/null
@@ -1,9 +0,0 @@
- global main
-
- section .text
-
-main:
- push rbp
- mov rbp, rsp
- sub rsp, 0,
-
\ No newline at end of file
diff --git a/build/test.asm b/build/test.asm
deleted file mode 100644
index 48dbddd..0000000
--- a/build/test.asm
+++ /dev/null
@@ -1,11 +0,0 @@
- global main
-
- section .text
-
-main:
- push rbp
- mov rbp, rsp
- sub rsp, 0
- mov eax, 12
- leave
- ret
\ No newline at end of file
diff --git a/build/test.o b/build/test.o
deleted file mode 100644
index 807975b..0000000
Binary files a/build/test.o and /dev/null differ
diff --git a/libs/kernel32.lib b/libs/kernel32.lib
deleted file mode 100644
index a5db285..0000000
Binary files a/libs/kernel32.lib and /dev/null differ
diff --git a/libs/legacy_stdio_definitions.lib b/libs/legacy_stdio_definitions.lib
deleted file mode 100644
index adbd07b..0000000
Binary files a/libs/legacy_stdio_definitions.lib and /dev/null differ
diff --git a/libs/legacy_stdio_wide_specifiers.lib b/libs/legacy_stdio_wide_specifiers.lib
deleted file mode 100644
index 0de905f..0000000
Binary files a/libs/legacy_stdio_wide_specifiers.lib and /dev/null differ
diff --git a/libs/msvcrt.lib b/libs/msvcrt.lib
deleted file mode 100644
index 5f58764..0000000
Binary files a/libs/msvcrt.lib and /dev/null differ
diff --git a/libs/ucrt.lib b/libs/ucrt.lib
deleted file mode 100644
index fd88b49..0000000
Binary files a/libs/ucrt.lib and /dev/null differ
diff --git a/libs/vcruntime.lib b/libs/vcruntime.lib
deleted file mode 100644
index 2c2ba22..0000000
Binary files a/libs/vcruntime.lib and /dev/null differ
diff --git a/main.why b/main.why
index 6d970aa..7518705 100644
--- a/main.why
+++ b/main.why
@@ -1,12 +1,3 @@
-struct A {
- b: int,
- c: int
-}
-
-fn check() -> A {
-
-}
-
fn main() -> int {
- printb(true);
+ return 12;
}
diff --git a/build/run.sh b/reference/run.sh
similarity index 100%
rename from build/run.sh
rename to reference/run.sh
diff --git a/src/root.rs b/src/root.rs
index b1dc267..a018520 100644
--- a/src/root.rs
+++ b/src/root.rs
@@ -10,7 +10,8 @@ use std::io::ErrorKind;
use std::path::PathBuf;
use crate::root::compiler::compile::compile;
use crate::root::name_resolver::resolve::resolve;
-use crate::root::shared::types::ByteSize;
+use shared::common::ByteSize;
+use crate::root::runner::{assemble, link_gcc, run};
// #[cfg(target_os = "windows")]
// use crate::root::runner::run;
@@ -84,47 +85,26 @@ pub fn main_args(args: Args) {
fs::write(PathBuf::from(format!("{}.asm", &args.output)), assembly.as_bytes()).unwrap();
);
- // print!("Compiling... ");
- // time!(generate_assembly(&args.output, functions););
- //
- // print!("Assembling (NASM)... ");
- // time!(if assemble(&args.output).is_err() {
- // return Err(AnyError::Other);
- // });
- // #[cfg(target_os = "windows")]
- // {
- // println!("Linking (MSVC - link.exe)... ");
- // time!(if link(&args.output).is_err() {
- // return Err(AnyError::Other);
- // });
- // if args.build {
- // println!("Skipping execution")
- // } else {
- // println!("Executing... ");
- // run(&args.output);
- // }
- // }
- // #[cfg(target_os = "linux")]
- // {
- // cprintln!("Compilation and execution on Linux may be buggy!>");
- // println!("Linking (gcc)... ");
- // time!(
- // let res = link_gcc_experimental(&args.output);
- // if res.is_err() {
- // return Err(AnyError::Other);
- // }
- // );
- //
- // if args.build {
- // println!("Skipping execution")
- // } else {
- // println!("Executing (wine)... ");
- // if run_wine_experimental(&args.output).is_err() {
- // return Err(AnyError::Other);
- // }
- // }
- // }
+ print!("Assembling (NASM)... ");
+ time!(
+ assemble(&args.output).unwrap();
+ );
+
+ #[cfg(target_os = "linux")]
+ {
+ println!("Linking (gcc)... ");
+ time!(
+ link_gcc(&args.output).unwrap();
+ );
+
+ if args.build {
+ println!("Skipping execution")
+ } else {
+ println!("Executing...");
+ run(&args.output);
+ }
+ }
cprintln!("Done!>");
}
diff --git a/src/root/builtin/int.rs b/src/root/builtin/int.rs
index 7503b2d..2960116 100644
--- a/src/root/builtin/int.rs
+++ b/src/root/builtin/int.rs
@@ -1,5 +1,8 @@
use unique_type_id::UniqueTypeId;
-use crate::root::shared::types::{ByteSize, Type, TypeID};
+use crate::root::compiler::assembly::utils::get_qword_stack_pointer;
+use crate::root::parser::parse_function::parse_literal::{LiteralToken, LiteralTokens};
+use crate::root::shared::common::{AddressedTypeRef, ByteSize, LocalAddress, TypeID};
+use crate::root::shared::types::Type;
#[derive(UniqueTypeId)]
#[UniqueTypeIdType = "u16"]
@@ -13,4 +16,21 @@ impl Type for IntType {
fn size(&self) -> ByteSize {
ByteSize(8)
}
+
+ fn instantiate_from_literal(&self, location: &LocalAddress, literal: &LiteralToken) -> String {
+ let location = get_qword_stack_pointer(location);
+ match literal.literal() {
+ LiteralTokens::Bool(value) => {
+ if *value {
+ format!("\tmov {location}, 0")
+ }
+ else {
+ format!("\tmov {location}, 1")
+ }
+ }
+ LiteralTokens::Int(value) => {
+ format!("\tmov {location}, {value}")
+ }
+ }
+ }
}
\ No newline at end of file
diff --git a/src/root/builtin/mod.rs b/src/root/builtin/mod.rs
index 5216691..c988f12 100644
--- a/src/root/builtin/mod.rs
+++ b/src/root/builtin/mod.rs
@@ -1,4 +1,4 @@
-mod int;
+pub mod int;
use crate::root::builtin::int::IntType;
use crate::root::name_resolver::name_resolvers::{GlobalDefinitionTable, ImplNode};
diff --git a/src/root/compiler/assembly/utils.rs b/src/root/compiler/assembly/utils.rs
index 6250d4a..e351ea3 100644
--- a/src/root/compiler/assembly/utils.rs
+++ b/src/root/compiler/assembly/utils.rs
@@ -1,4 +1,4 @@
-use crate::root::shared::types::{ByteSize, FunctionID};
+use crate::root::shared::common::{ByteSize, FunctionID, LocalAddress};
pub fn get_function_tag(id: FunctionID) -> String {
if id.is_main() {
@@ -44,4 +44,14 @@ pub fn align_16_bytes_plus_8(bytes: ByteSize) -> ByteSize {
} else {
ByteSize(bytes + (16 % (24 - (bytes % 16))))
}
+}
+
+pub fn get_qword_stack_pointer(address: &LocalAddress) -> String {
+ let address = address.0;
+
+ if address >= 0 {
+ format!("qword [rbp+{address}]")
+ } else {
+ format!("qword [rbp{address}]")
+ }
}
\ No newline at end of file
diff --git a/src/root/compiler/compile.rs b/src/root/compiler/compile.rs
index f5d735e..ccc8acc 100644
--- a/src/root/compiler/compile.rs
+++ b/src/root/compiler/compile.rs
@@ -2,7 +2,7 @@ use std::collections::{HashMap, HashSet};
use crate::root::compiler::compile_function::compile_function;
use crate::root::name_resolver::name_resolvers::GlobalDefinitionTable;
use crate::root::parser::parse_function::FunctionToken;
-use crate::root::shared::types::FunctionID;
+use crate::root::shared::common::FunctionID;
pub fn compile(global_table: GlobalDefinitionTable, unprocessed_functions: HashMap) -> String {
let mut unprocessed_functions = unprocessed_functions;
diff --git a/src/root/compiler/compile_evaluable.rs b/src/root/compiler/compile_evaluable.rs
new file mode 100644
index 0000000..7ac9623
--- /dev/null
+++ b/src/root/compiler/compile_evaluable.rs
@@ -0,0 +1,37 @@
+use std::collections::HashSet;
+use crate::root::compiler::local_variable_table::LocalVariableTable;
+use crate::root::name_resolver::name_resolvers::GlobalDefinitionTable;
+use crate::root::parser::parse_function::parse_evaluable::{EvaluableToken, EvaluableTokens};
+use crate::root::shared::common::{FunctionID, TypeRef};
+use crate::root::shared::common::AddressedTypeRef;
+
+pub fn compile_evaluable(fid: FunctionID, et: &EvaluableToken, target: Option, local_variables: &mut LocalVariableTable, global_table: &GlobalDefinitionTable, function_calls: &mut HashSet) -> (String, Option) {
+ let et = et.token();
+
+ match et {
+ EvaluableTokens::Name(_) => todo!(),
+ EvaluableTokens::Literal(literal) => {
+ let (address, t, tid) = if let Some(target) = target {
+ let (address, tid) = target.dissolve();
+ if tid.indirection().has_indirection() {
+ todo!()
+ }
+ let t = global_table.type_definitions().get(tid.type_id()).unwrap();
+ (address, t, tid)
+ }
+ else {
+ let tid = literal.literal().default_type();
+ if tid.indirection().has_indirection() {
+ todo!()
+ }
+ let t = global_table.type_definitions().get(tid.type_id()).unwrap();
+ let address = local_variables.add_new_unnamed(t.size());
+ (address, t, tid)
+ };
+
+ (t.instantiate_from_literal(&address, literal), Some(AddressedTypeRef::new(address, tid)))
+ }
+ EvaluableTokens::InfixOperator(_, _, _) => todo!(),
+ EvaluableTokens::PrefixOperator(_, _) => todo!()
+ }
+}
\ No newline at end of file
diff --git a/src/root/compiler/compile_function.rs b/src/root/compiler/compile_function.rs
index cc1195f..6ae7cfb 100644
--- a/src/root/compiler/compile_function.rs
+++ b/src/root/compiler/compile_function.rs
@@ -1,14 +1,12 @@
use std::collections::HashSet;
-use crate::root::compiler::assembly::utils::{align_16_bytes, align_16_bytes_plus_8, get_function_tag};
+use crate::root::compiler::assembly::utils::{align_16_bytes, align_16_bytes_plus_8, get_function_tag, get_qword_stack_pointer};
+use crate::root::compiler::compile_evaluable::compile_evaluable;
use crate::root::compiler::local_variable_table::LocalVariableTable;
use crate::root::name_resolver::name_resolvers::{GlobalDefinitionTable, NameResultId};
-use crate::root::parser::parse::Location;
use crate::root::parser::parse_function::FunctionToken;
use crate::root::parser::parse_function::parse_line::LineTokens;
-use crate::root::parser::parse_function::parse_operator::get_priority;
-use crate::root::parser::parse_name::UnresolvedNameToken;
-use crate::root::parser::parse_parameters::Parameters;
-use crate::root::shared::types::{AddressedTypeRef, FunctionID, LocalAddress, TypeID};
+use crate::root::shared::common::{FunctionID, LocalAddress};
+use crate::root::shared::common::AddressedTypeRef;
pub fn compile_function(fid: FunctionID, function: FunctionToken, global_table: &GlobalDefinitionTable) -> (String, HashSet) {
let mut local_variables = Box::new(LocalVariableTable::default());
@@ -29,7 +27,7 @@ pub fn compile_function(fid: FunctionID, function: FunctionToken, global_table:
NameResultId::NotFound => todo!()
}));
- let mut param_address = LocalAddress(-8);
+ let mut param_address = LocalAddress(8);
for (param_name, param_type) in parameters {
let type_ref = match global_table.resolve_global_name_to_id(¶m_type).unwrap().unwrap() {
@@ -44,26 +42,26 @@ pub fn compile_function(fid: FunctionID, function: FunctionToken, global_table:
};
let size = global_table.type_definitions().get(type_ref.type_id()).unwrap().size();
- param_address -= LocalAddress(size.0 as isize);
-
local_variables.add_existing(param_name, AddressedTypeRef::new(param_address, type_ref));
+
+ param_address += LocalAddress(size.0 as isize);
}
let return_variable = return_type.and_then(
|t| Some(AddressedTypeRef::new(
- param_address - LocalAddress(global_table.type_definitions().get(t.type_id()).unwrap().size().0 as isize),
+ param_address,// - LocalAddress(global_table.type_definitions().get(t.type_id()).unwrap().size().0 as isize),
t
))
);
let mut function_calls = HashSet::new();
- let (full_contents, local_variables) = recursively_compile_lines(fid, &lines, return_variable, local_variables, global_table, &mut function_calls);
+ let (full_contents, local_variables) = recursively_compile_lines(fid, &lines, &return_variable, local_variables, global_table, &mut function_calls);
let stack_size = local_variables.stack_size();
- let final_contents = format!(
+ let mut final_contents = format!(
"{}:
push rbp
mov rbp, rsp
@@ -74,32 +72,40 @@ pub fn compile_function(fid: FunctionID, function: FunctionToken, global_table:
full_contents
);
+ if fid.is_main() {
+ final_contents += "\n\tleave\n\tret"
+ }
+
(final_contents, function_calls)
}
-fn recursively_compile_lines(fid: FunctionID, lines: &[LineTokens], return_variable: Option, local_variables: Box, global_table: &GlobalDefinitionTable, function_calls: &mut HashSet) -> (String, Box) {
- let local_variables = local_variables.enter_block();
+fn recursively_compile_lines(fid: FunctionID, lines: &[LineTokens], return_variable: &Option, local_variables: Box, global_table: &GlobalDefinitionTable, function_calls: &mut HashSet) -> (String, Box) {
+ let mut local_variables = local_variables.enter_block();
let mut contents = String::new();
for line in lines {
match line {
- LineTokens::Initialisation(_) => {
-
- }
- LineTokens::Assignment(_) => {
-
- }
- LineTokens::If(_) => {
-
- }
- LineTokens::While(_) => {
-
- }
+ LineTokens::Initialisation(_) => todo!(),
+ LineTokens::Assignment(_) => todo!(),
+ LineTokens::If(_) => todo!(),
+ LineTokens::While(_) => todo!(),
LineTokens::Return(rt) => {
-
+ if fid.is_main() {
+ let (code, location) = compile_evaluable(fid, rt.return_value(), None, &mut local_variables, global_table, function_calls);
+ let location = location.unwrap();
+ contents += "\n";
+ contents += &code;
+ contents += &format!("\n\tmov rax, {}", get_qword_stack_pointer(location.local_address()));
+ }
+ else {
+ todo!()
+ }
+ }
+ LineTokens::Break(_) => todo!(),
+ LineTokens::NoOp(et) => {
+ contents += "\n";
+ contents += &compile_evaluable(fid, et, None, &mut local_variables, global_table, function_calls).0;
}
- LineTokens::Break(_) => {}
- LineTokens::NoOp(_) => {}
}
}
diff --git a/src/root/compiler/local_variable_table.rs b/src/root/compiler/local_variable_table.rs
index a55af8d..c650466 100644
--- a/src/root/compiler/local_variable_table.rs
+++ b/src/root/compiler/local_variable_table.rs
@@ -1,5 +1,6 @@
use std::collections::HashMap;
-use crate::root::shared::types::{AddressedTypeRef, ByteSize, Type, TypeID};
+use crate::root::shared::common::{AddressedTypeRef, ByteSize, LocalAddress, TypeID};
+use crate::root::shared::types::Type;
/// Function-local table of defined variables. Only used within function processing
#[derive(Default)]
@@ -34,6 +35,11 @@ impl LocalVariableTable {
self.table.insert(name, addressed_type_ref);
}
+ pub fn add_new_unnamed(&mut self, size: ByteSize) -> LocalAddress {
+ self.stack_size += size;
+ LocalAddress(-(self.stack_size.0 as isize))
+ }
+
pub fn get_ref(&self, name: &str) -> Option {
if let Some(r) = self.table.get(name) {
Some(r.clone())
diff --git a/src/root/compiler/mod.rs b/src/root/compiler/mod.rs
index c864dd3..f98fc7e 100644
--- a/src/root/compiler/mod.rs
+++ b/src/root/compiler/mod.rs
@@ -1,4 +1,5 @@
pub mod compile;
pub mod local_variable_table;
mod compile_function;
-mod assembly;
\ No newline at end of file
+pub mod assembly;
+mod compile_evaluable;
\ 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 4749992..577c6bd 100644
--- a/src/root/name_resolver/name_resolvers.rs
+++ b/src/root/name_resolver/name_resolvers.rs
@@ -4,10 +4,11 @@ use std::rc::Rc;
use derive_getters::Getters;
use crate::root::compiler::local_variable_table::LocalVariableTable;
use crate::root::name_resolver::resolve_function_signatures::FunctionSignature;
-use crate::root::shared::types::{AddressedTypeRef, FunctionID, Type, TypeID, TypeRef};
+use crate::root::shared::types::Type;
use crate::root::parser::parse_function::FunctionToken;
use crate::root::parser::parse_name::UnresolvedNameToken;
use crate::root::parser::parse_struct::StructToken;
+use crate::root::shared::common::{AddressedTypeRef, FunctionID, TypeID, TypeRef};
#[derive(Default)]
pub struct ImplNode {
diff --git a/src/root/name_resolver/resolve.rs b/src/root/name_resolver/resolve.rs
index efa75ce..a720431 100644
--- a/src/root/name_resolver/resolve.rs
+++ b/src/root/name_resolver/resolve.rs
@@ -4,7 +4,7 @@ use crate::root::name_resolver::name_resolvers::GlobalDefinitionTable;
use crate::root::name_resolver::resolve_names::resolve_names;
use crate::root::parser::parse_function::FunctionToken;
use crate::root::parser::parse_toplevel::TopLevelTokens;
-use crate::root::shared::types::FunctionID;
+use crate::root::shared::common::FunctionID;
pub fn resolve(ast: Vec) -> (GlobalDefinitionTable, HashMap) {
let mut global_table = GlobalDefinitionTable::new();
diff --git a/src/root/name_resolver/resolve_function_signatures.rs b/src/root/name_resolver/resolve_function_signatures.rs
index 6a71a07..d776b55 100644
--- a/src/root/name_resolver/resolve_function_signatures.rs
+++ b/src/root/name_resolver/resolve_function_signatures.rs
@@ -1,6 +1,6 @@
use derive_getters::Getters;
use crate::root::name_resolver::name_resolvers::{GlobalDefinitionTable, NameResult, NameResultId};
-use crate::root::shared::types::TypeRef;
+use crate::root::shared::common::TypeRef;
use crate::root::parser::parse_function::FunctionToken;
#[derive(Getters)]
diff --git a/src/root/name_resolver/resolve_names.rs b/src/root/name_resolver/resolve_names.rs
index 026904a..df18f26 100644
--- a/src/root/name_resolver/resolve_names.rs
+++ b/src/root/name_resolver/resolve_names.rs
@@ -1,14 +1,18 @@
use std::collections::HashMap;
+
use derive_getters::Getters;
use itertools::Itertools;
+
use crate::root::name_resolver::name_resolvers::{GlobalDefinitionTable, NameResultId};
-use crate::root::shared::types::{ByteSize, FunctionID, TypeID, TypeRef};
use crate::root::name_resolver::resolve_function_signatures::resolve_function_signature;
use crate::root::name_resolver::resolve_type_sizes::{resolve_type_sizes, UnsizedUserType};
use crate::root::parser::parse::Location;
use crate::root::parser::parse_function::FunctionToken;
+use crate::root::parser::parse_function::parse_literal::LiteralToken;
use crate::root::parser::parse_name::UnresolvedNameToken;
use crate::root::parser::parse_toplevel::TopLevelTokens;
+use crate::root::shared::common::{LocalAddress, TypeRef};
+use crate::root::shared::common::{ByteSize, FunctionID, TypeID};
use crate::root::shared::types::Type;
// #[derive(Hash)]
@@ -73,6 +77,10 @@ impl Type for UserType {
fn size(&self) -> ByteSize {
self.size
}
+
+ fn instantiate_from_literal(&self, location: &LocalAddress, literal: &LiteralToken) -> String {
+ todo!()
+ }
}
// ! Unoptimised
diff --git a/src/root/name_resolver/resolve_type_sizes.rs b/src/root/name_resolver/resolve_type_sizes.rs
index b7c7fa0..ed29a5d 100644
--- a/src/root/name_resolver/resolve_type_sizes.rs
+++ b/src/root/name_resolver/resolve_type_sizes.rs
@@ -1,10 +1,11 @@
use std::collections::HashMap;
-use derive_getters::{Dissolve, Getters};
+use derive_getters::Dissolve;
use crate::root::name_resolver::name_resolvers::GlobalDefinitionTable;
-use crate::root::shared::types::{ByteSize, TypeID, TypeRef};
+use crate::root::shared::common::TypeRef;
use crate::root::name_resolver::resolve_names::UserType;
use crate::root::parser::parse::Location;
use crate::root::POINTER_SIZE;
+use crate::root::shared::common::{ByteSize, TypeID};
#[derive(Dissolve)]
pub struct UnsizedUserType {
diff --git a/src/root/parser/parse_function/parse_evaluable.rs b/src/root/parser/parse_function/parse_evaluable.rs
index b59cbfc..104e663 100644
--- a/src/root/parser/parse_function/parse_evaluable.rs
+++ b/src/root/parser/parse_function/parse_evaluable.rs
@@ -5,12 +5,13 @@ use crate::root::parser::parse_function::parse_literal::{
use crate::root::parser::parse_function::parse_operator::{parse_operator, OperatorToken};
use crate::root::parser::parse_name::{parse_full_name, UnresolvedNameToken};
use b_box::b;
+use derive_getters::Getters;
use nom::branch::alt;
use nom::character::complete::{char};
use crate::root::parser::parse_blocks::default_section;
use crate::root::parser::parse_util::discard_ignored;
-#[derive(Debug)]
+#[derive(Debug, Getters)]
pub struct EvaluableToken {
location: Location,
token: EvaluableTokens,
@@ -24,7 +25,7 @@ pub fn temp_from_token(s: Span, token: EvaluableTokens) -> TempEvaluableTokens {
}
#[derive(Debug)]
-enum EvaluableTokens {
+pub enum EvaluableTokens {
Name(UnresolvedNameToken),
Literal(LiteralToken),
InfixOperator(Box, OperatorToken, Box),
diff --git a/src/root/parser/parse_function/parse_literal.rs b/src/root/parser/parse_function/parse_literal.rs
index 212ba66..a29c8f5 100644
--- a/src/root/parser/parse_function/parse_literal.rs
+++ b/src/root/parser/parse_function/parse_literal.rs
@@ -1,9 +1,13 @@
+use derive_getters::{Dissolve, Getters};
use crate::root::parser::parse::{Location, ParseResult, Span};
use nom::branch::alt;
use nom::bytes::complete::tag;
+use crate::root::builtin::int::IntType;
use crate::root::parser::parse_util::discard_ignored;
+use crate::root::shared::common::{Indirection, TypeID, TypeRef};
+use crate::root::shared::types::Type;
-#[derive(Debug)]
+#[derive(Debug, Dissolve, Getters)]
pub struct LiteralToken {
location: Location,
literal: LiteralTokens,
@@ -15,6 +19,19 @@ pub enum LiteralTokens {
Int(i64),
}
+impl LiteralTokens {
+ pub fn default_type(&self) -> TypeRef {
+ match self {
+ LiteralTokens::Bool(_) => {
+ todo!()
+ }
+ LiteralTokens::Int(_) => {
+ TypeRef::new(IntType{}.id(), Indirection(0))
+ }
+ }
+ }
+}
+
pub fn parse_literal(s: Span) -> ParseResult {
let (s, _) = discard_ignored(s)?;
diff --git a/src/root/parser/parse_name.rs b/src/root/parser/parse_name.rs
index 662ada8..24500b4 100644
--- a/src/root/parser/parse_name.rs
+++ b/src/root/parser/parse_name.rs
@@ -10,7 +10,7 @@ use nom_supreme::error::{BaseErrorKind, Expectation};
use crate::root::parser::parse_arguments::parse_arguments;
use crate::root::parser::parse_blocks::{default_section, section};
use crate::root::parser::parse_util::discard_ignored;
-use crate::root::shared::types::Indirection;
+use crate::root::shared::common::Indirection;
#[derive(Debug)]
pub enum NameConnectors {
diff --git a/src/root/parser/parse_struct.rs b/src/root/parser/parse_struct.rs
index 8eb9454..7cc733d 100644
--- a/src/root/parser/parse_struct.rs
+++ b/src/root/parser/parse_struct.rs
@@ -12,7 +12,7 @@ use nom_supreme::tag::complete::tag;
use substring::Substring;
use crate::root::parser::parse_blocks::default_section;
use crate::root::parser::parse_util::{discard_ignored, require_ignored};
-use crate::root::shared::types::TypeID;
+use crate::root::shared::common::TypeID;
#[derive(Debug, Getters, Dissolve)]
pub struct StructToken {
diff --git a/src/root/runner.rs b/src/root/runner.rs
index 74af1b1..cd6e52c 100644
--- a/src/root/runner.rs
+++ b/src/root/runner.rs
@@ -7,11 +7,11 @@ use color_print::cprintln;
use crate::ret_time;
use crate::root::utils::try_run_program;
-#[cfg(target_os = "windows")]
+#[cfg(target_os = "linux")]
pub fn run(output: &str) {
let time;
ret_time!(time,
- let full = fs::canonicalize(format!("{output}.exe")).unwrap();
+ let full = fs::canonicalize(format!("{output}.out")).unwrap();
let code = match Command::new(full).status() {
Ok(r) => {
match r.code() {
@@ -36,26 +36,11 @@ pub fn run(output: &str) {
cprintln!("Completed [{:?}]>", time);
}
-#[cfg(target_os = "linux")]
-pub fn run_wine_experimental(output: &str) -> Result<(), ()> {
- let time;
- ret_time!(time,
- let full = fs::canonicalize(format!("{output}.exe")).unwrap();
- let code = try_run_program("wine", Command::new("wine").args([full]).status())?
- .code()
- .unwrap();
- );
-
- println!("\nExited with return code {}", code);
- cprintln!("Completed [{:?}]>", time);
- Ok(())
-}
-
pub fn assemble(output: &str) -> Result<(), ()> {
if !try_run_program(
"nasm",
Command::new("nasm")
- .args(["-f", "win64", format!("{output}.asm").as_str()])
+ .args(["-f", "elf64", format!("{output}.asm").as_str()])
.status(),
)?
.success()
@@ -66,45 +51,16 @@ pub fn assemble(output: &str) -> Result<(), ()> {
Ok(())
}
-#[cfg(target_os = "windows")]
-pub fn link(output: &str) -> Result<(), ()> {
- if !try_run_program(
- "link",
- Command::new("link")
- .args([
- // "/entry:main",
- format!("/out:{output}.exe").as_str(),
- "/SUBSYSTEM:CONSOLE",
- // "/LARGEADDRESSAWARE:NO",
- format!("{output}.obj").as_str(),
- ".\\libs\\kernel32.lib",
- ".\\libs\\msvcrt.lib",
- ".\\libs\\legacy_stdio_definitions.lib",
- ".\\libs\\legacy_stdio_wide_specifiers.lib",
- ".\\libs\\vcruntime.lib",
- ".\\libs\\ucrt.lib",
- ])
- .status(),
- )?
- .success()
- {
- cprintln!("MSVC linking step failed>");
- return Err(());
- }
-
- Ok(())
-}
#[cfg(target_os = "linux")]
-pub fn link_gcc_experimental(output: &str) -> Result<(), ()> {
+pub fn link_gcc(output: &str) -> Result<(), ()> {
if !try_run_program(
- "x86_64-w64-mingw32-gcc",
- Command::new("x86_64-w64-mingw32-gcc")
+ "gcc",
+ Command::new("gcc")
.args([
- format!("{output}.obj").as_str(),
- "./libs/kernel32.lib",
+ format!("{output}.o").as_str(),
"-o",
- format!("{output}.exe").as_str(),
+ format!("{output}.out").as_str(),
])
.status(),
)?
diff --git a/src/root/shared/common.rs b/src/root/shared/common.rs
new file mode 100644
index 0000000..0bd533d
--- /dev/null
+++ b/src/root/shared/common.rs
@@ -0,0 +1,61 @@
+use derive_more::{Add, AddAssign, Display, Sub, SubAssign};
+use derive_getters::{Dissolve, Getters};
+
+#[derive(Debug, PartialEq, Eq, Hash, Display, Copy, Clone)]
+#[display(fmt = "TypeID: {}", .0)]
+pub struct TypeID(pub isize);
+
+#[derive(Debug, PartialEq, Eq, Hash, Display, Copy, Clone)]
+#[display(fmt = "FunctionID: {}", .0)]
+pub struct FunctionID(pub isize);
+
+impl FunctionID {
+ pub fn is_main(&self) -> bool {
+ self.0 == 0
+ }
+}
+
+#[derive(Debug, PartialEq, Eq, Hash, Display, Copy, Clone)]
+#[derive(Add, AddAssign, Sub, SubAssign)]
+#[display(fmt = "Indirection: {}", .0)]
+pub struct Indirection(pub usize);
+
+impl Indirection {
+ pub fn has_indirection(&self) -> bool {
+ self.0 != 0
+ }
+}
+
+#[derive(Debug, PartialEq, Eq, Hash, Display, Copy, Clone, Default)]
+#[derive(Add, AddAssign, Sub, SubAssign)]
+#[display(fmt = "ByteSize: {}", .0)]
+pub struct ByteSize(pub usize);
+
+#[derive(Debug, PartialEq, Eq, Hash, Display, Copy, Clone)]
+#[derive(Add, AddAssign, Sub, SubAssign)]
+#[display(fmt = "LocalAddress: {}", .0)]
+pub struct LocalAddress(pub isize);
+
+#[derive(Getters, Clone)]
+pub struct TypeRef {
+ type_id: TypeID,
+ indirection: Indirection
+}
+
+impl TypeRef {
+ pub fn new(type_id: TypeID, indirection: Indirection) -> TypeRef {
+ TypeRef { type_id, indirection }
+ }
+}
+
+#[derive(Getters, Clone, Dissolve)]
+pub struct AddressedTypeRef {
+ local_address: LocalAddress,
+ type_ref: TypeRef
+}
+
+impl AddressedTypeRef {
+ pub fn new(local_address: LocalAddress, type_ref: TypeRef) -> AddressedTypeRef {
+ AddressedTypeRef { local_address, type_ref }
+ }
+}
diff --git a/src/root/shared/mod.rs b/src/root/shared/mod.rs
index dd198c6..3bf1a09 100644
--- a/src/root/shared/mod.rs
+++ b/src/root/shared/mod.rs
@@ -1 +1,2 @@
-pub mod types;
\ No newline at end of file
+pub mod types;
+pub(crate) mod common;
diff --git a/src/root/shared/types.rs b/src/root/shared/types.rs
index 72dde92..a40cea0 100644
--- a/src/root/shared/types.rs
+++ b/src/root/shared/types.rs
@@ -1,67 +1,10 @@
-use derive_getters::Getters;
-use derive_more::{Add, AddAssign, Display, Div, Sub, SubAssign};
-
-#[derive(Debug, PartialEq, Eq, Hash, Display, Copy, Clone)]
-#[display(fmt = "TypeID: {}", .0)]
-pub struct TypeID(pub isize);
-
-#[derive(Debug, PartialEq, Eq, Hash, Display, Copy, Clone)]
-#[display(fmt = "FunctionID: {}", .0)]
-pub struct FunctionID(pub isize);
-
-impl FunctionID {
- pub fn is_main(&self) -> bool {
- self.0 == 0
- }
-}
-
-#[derive(Debug, PartialEq, Eq, Hash, Display, Copy, Clone)]
-#[derive(Add, AddAssign, Sub, SubAssign)]
-#[display(fmt = "Indirection: {}", .0)]
-pub struct Indirection(pub usize);
-
-impl Indirection {
- pub fn has_indirection(&self) -> bool {
- self.0 != 0
- }
-}
-
-#[derive(Debug, PartialEq, Eq, Hash, Display, Copy, Clone, Default)]
-#[derive(Add, AddAssign, Sub, SubAssign)]
-#[display(fmt = "ByteSize: {}", .0)]
-pub struct ByteSize(pub usize);
-
-#[derive(Debug, PartialEq, Eq, Hash, Display, Copy, Clone)]
-#[derive(Add, AddAssign, Sub, SubAssign)]
-#[display(fmt = "LocalAddress: {}", .0)]
-pub struct LocalAddress(pub isize);
+use crate::root::parser::parse_function::parse_literal::LiteralToken;
+use crate::root::shared::common::{AddressedTypeRef, ByteSize, LocalAddress, TypeID};
pub trait Type {
fn id(&self) -> TypeID;
fn size(&self) -> ByteSize;
-}
-
-#[derive(Getters, Clone)]
-pub struct TypeRef {
- type_id: TypeID,
- indirection: Indirection
-}
-
-impl TypeRef {
- pub fn new(type_id: TypeID, indirection: Indirection) -> TypeRef {
- TypeRef { type_id, indirection }
- }
-}
-
-#[derive(Getters, Clone)]
-pub struct AddressedTypeRef {
- local_address: LocalAddress,
- type_ref: TypeRef
-}
-impl AddressedTypeRef {
- pub fn new(local_address: LocalAddress, type_ref: TypeRef) -> AddressedTypeRef {
- AddressedTypeRef { local_address, type_ref }
- }
+ fn instantiate_from_literal(&self, location: &LocalAddress, literal: &LiteralToken) -> String;
}