From b327756f4d0916c182ac6da0c5b341775a57aa48 Mon Sep 17 00:00:00 2001 From: MCorange Date: Thu, 13 Apr 2023 02:38:10 +0300 Subject: [PATCH 1/4] Fixed vscode highlighting --- editor/vscode/mclang-0.0.2.vsix | Bin 4818 -> 0 bytes editor/vscode/mclang-0.0.3.vsix | Bin 0 -> 4807 bytes editor/vscode/package.json | 2 +- editor/vscode/syntaxes/mclang.tmLanguage.json | 2 +- 4 files changed, 2 insertions(+), 2 deletions(-) delete mode 100644 editor/vscode/mclang-0.0.2.vsix create mode 100644 editor/vscode/mclang-0.0.3.vsix diff --git a/editor/vscode/mclang-0.0.2.vsix b/editor/vscode/mclang-0.0.2.vsix deleted file mode 100644 index a81eedd34b944d0c58aa7ab6c1b2c157bc701eba..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 4818 zcmai&2UJtr(#JzD(t8o4BTAPhibyBa(2GbSN zSE))znv{UFFWh_a%3a@kZ?aBylC|<*XJ*dK{>=udk4N`rHv85LIO@0=Zk9V9M$HbO?}wyb zq5W{&)xBnUytW^DMcR7qiIT9LTGuD(O@F5W5FeC&v(ocJO)6YO=W z!1%`|`M45d9A06m2p_N>9fyx&;}er7GaHU%7W`SpUl)X@r_Gp)o4%yHfcfyhXRes2 zO1q&g|q|$iR>P8 zLLxaR*txZXKbmHShb5C%iWlLoaa4V~0Y^94X=s_ZhLCx-J*^KH&S8M@+#=~LR^B?) zvrw70MHLOosAB28k-w`Ma>UeFJ3oWFoCd1J?oeFUNmJ4x6YrW}Y~*ASu79I{b&UogdOaTC0y0GAuUe>&NT1V5M}Z{svPv6HH?m5 zU+T_&Z5xo^m)T^R7{8vgQ$HV_LO6L=Jtj1nGfxq*(OasUYa`&T0@S~Y@L8idmEo+2 zU@~Hah-}n$^p;P~e{w4jp=Rmv;53nVPH+e#8q|^SW#DFC7g>q%&z0ODAPR`y=C5{* zYzYs*?H92|J1u$lU3+}3R(vT(G@22;x^!5gtIJL8^kYTV;7AB`sU*_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K0*IO${Q8xq z#?@10gj;e#DIv-DAMf5zzFz%!hRBaGr)n>BUWF}1pJ&dmJ?0}fg+k#jO>8@b_;GY7 zgsf6yOj7F3x-~`U0FQ7BJuM4Q@hssfDHMNB2lok~91XA91aOutF*uraa+^anA*Ze5 zi{x}S2QOeoevISfCv89GT_$d(<~4C5`*azVpCG6=z$@K8F_fiwyo<@5)MwoQ4P8bH z2}-+gJUGdiSvOxKN%Ag+E0@nOzT{MC>aj2MrO}byO}n>o<#4Ax#8>r0WK6nAJ>R`P z#jf|duE@jv>V1|%<)-%PV_VJAhIeB#2TeqQbSzr>i*sx3>M-X{DuGsNb(P}wt;|^{ z%o-@EJk4LLv7;K?^&?)aY1CwuD~vIU_dSAn0DAqagB^V=!8N5g2F|45b_7icSDG-* zd>!?y!*|@F2T_LGpe5FH9Qqfh;H~W+yEN!!!<{Cu`{KllTkcRTPnB;F{w}(q{?6e? z*!`$+CxY%;ryy!}tB)_A2Kdh{mOCI{X!m5lUhW3gEmmf+ZD(Z3tI!&AXe^}iKZx!H z#z;6D5i!zG@mCB*C7Xb^IFjJI+7hNxd#p*R?Rp|}2R|hE7g^22B^7N&?01}M?^p%P zgnv+z@gYBoJ}aTgiZ~o5nHm-m;G~+MR8yaIRE(w0<$guY2`uZ%P-crbRh9>jepNn1MEMHdt9|3P6Obv(>HV-Bw{uREBy1W}xS`#)kCL)ZWS diff --git a/editor/vscode/mclang-0.0.3.vsix b/editor/vscode/mclang-0.0.3.vsix new file mode 100644 index 0000000000000000000000000000000000000000..fc2c7ced7e0ba832eca22dcc8ab74a3e324dddf7 GIT binary patch literal 4807 zcmai22T)U6w+=P*-UI}sBfWQ(E+9y+(xmqmAP_*KfD|c;L6DB2gR~&sgf3k=3er)U zfFdP;@WQ=6Ub*xB_tu#+`{-k5wboEfxKh zim2P{unY=q-?s^Elimr0D) zaaDN~XnZF7y#;H8D_6A&834>Ayn&vTN!PNo7h%BdwBx4nnd9JcFr6~09v9TkF~-)b z=I4i@qSWD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1>*O73Ak{`qRjt#Tz8} zKFU)HJ(j>>>bCh9ob`9qo5^0;@MGOiKf4RRO&BvBEh#n4$sgmzlqxBad@{w^w0NvL z+C9s9Z{K3-;HV|h`PW3Ej~q+j$*B0|&4zcQCu6GF7I5b514tNhZsRR#J1Yo(=}Y%D zN}t(g1{;#bo6^5BycR*oj052(KBB9~_K5Mp;!-90CnQbT;f>ZLa2vWZX1b+yJI&uac{hVeMD8Q7}J&V=awNx1Q{*% z=wNXux~uB;aZc67uj`IA@&rrREi7wk8U(ds7vDO1o8m>Tn-FUUh0tN&SmLUo7-p*@ zr|5{?nhC<5drX*e`tXE7n;RA*Yx9HJi?D-&PZ7t#K$R4^)SA&QNWD)ws4Gd5ck2^_ z(_unh`!X$aD-t@$rVx9J;#%~gs;2zaCKaxN*Fb)2f~t|9s=>!yP3FtE^TY&}a2q5$ zqa_^4^8*rOlq-gsHh!9jyN>pLvs?C~@-go~8P=rFq;`Wr4|r{T*8JZfy+Q}gU-fBvVifVWX*DF<1dnH{PnX{8wXgyvM^3hP;dD{{H zzDx{EsjYZ4$tai+LT)&)Y$>{OI4dQv$YK>Mt?D4^yxZ3#ZyTW)i_lUCB0o+ztE9<` zLywbu9;X!Kp!vkb8O4`V2YFN{D-n;*ieTvuTUj^~KSFm6tzX|Kd4N16Jl7Mo?LAb& zKmb4-bKIb(jfH&^=f9sjV7B7F3;{hKe(Q@%rx5?f_;;W7Hv#~FfYM+T%>UB0{Ttxl z-OoP&XBdFrlep9iU0_@^8h;Ao^G4&s;Ls`DKQaD~w&PF83q^e)lg}0Pg~6f9_sEO1X1AePM9ulS>ePs_P4k|0en_Fc45~jFSH+I=?001;B;Y`ZNFMTI<5# d(49*F|Bv$0)5gQh8FONVx#Tf%38XlG`yXmNIT-)| literal 0 HcmV?d00001 diff --git a/editor/vscode/package.json b/editor/vscode/package.json index 90aeeec..25be400 100644 --- a/editor/vscode/package.json +++ b/editor/vscode/package.json @@ -2,7 +2,7 @@ "name": "mclang", "displayName": "mclang", "description": "Code highlighting for mclang", - "version": "0.0.2", + "version": "0.0.3", "repository": { "type": "git", "url": "git@github.com:mc-lang/mclang2.git" diff --git a/editor/vscode/syntaxes/mclang.tmLanguage.json b/editor/vscode/syntaxes/mclang.tmLanguage.json index 7fa7b7b..a70f660 100644 --- a/editor/vscode/syntaxes/mclang.tmLanguage.json +++ b/editor/vscode/syntaxes/mclang.tmLanguage.json @@ -112,7 +112,7 @@ ] }, "comments": { - "disabled__patterns": [ + "patterns": [ { "name": "constant.other.character-class.regexp", "match": "(?://\\s*)(TODO(O*)|FIXME).*" From c8547af24cbfe3d8c13eb3975840ba498462afa8 Mon Sep 17 00:00:00 2001 From: MCorange Date: Thu, 13 Apr 2023 02:39:21 +0300 Subject: [PATCH 2/4] implemented inline functions --- src/compile/linux_x86_64.rs | 11 +- src/compile/mod.rs | 3 +- src/constants.rs | 8 +- src/interpret/linux_x86_64/mod.rs | 5 +- src/parser.rs | 5 +- src/preprocessor.rs | 161 ++++++++++++++++++++++-------- src/typechecker.rs | 2 + test.mcl | 8 +- 8 files changed, 149 insertions(+), 54 deletions(-) diff --git a/src/compile/linux_x86_64.rs b/src/compile/linux_x86_64.rs index a87969e..79418af 100644 --- a/src/compile/linux_x86_64.rs +++ b/src/compile/linux_x86_64.rs @@ -92,10 +92,11 @@ pub fn compile(tokens: &[Operator], args: &Args) -> Result{ writeln!(writer, " ;; -- {:?}", token.typ)?; } } else { - - if ti != 0 && tokens[ti-1].typ == OpType::Keyword(KeywordType::Else) || + if ti > 0 { + if tokens[ti-1].typ == OpType::Keyword(KeywordType::Else) || tokens[ti-1].typ == OpType::Keyword(KeywordType::End){ - writeln!(writer, "addr_{ti}:")?; + writeln!(writer, "addr_{ti}:")?; + } } if ti + 1 < tokens.len() && tokens[ti+1].typ == OpType::Keyword(KeywordType::End) { @@ -496,7 +497,7 @@ pub fn compile(tokens: &[Operator], args: &Args) -> Result{ writeln!(writer, " pop rbx")?; writeln!(writer, " mov qword [rbp], rbx")?; writeln!(writer, " add rbp, 8")?; - functions.push(Function { loc: token.loc.clone(), name: token.text.clone() }); + functions.push(Function { loc: token.loc.clone(), name: token.text.clone(), exter: false}); ti += 1; }, KeywordType::FunctionDone => { @@ -509,7 +510,9 @@ pub fn compile(tokens: &[Operator], args: &Args) -> Result{ KeywordType::FunctionThen => ti += 1, KeywordType::Function | KeywordType::Include | + KeywordType::FunctionDefInline | KeywordType::Constant => unreachable!(), + KeywordType::Inline => todo!(), } } } diff --git a/src/compile/mod.rs b/src/compile/mod.rs index 8391aa0..542e6ed 100644 --- a/src/compile/mod.rs +++ b/src/compile/mod.rs @@ -23,5 +23,6 @@ pub struct Memory { #[derive(Debug, Clone)] pub struct Function { pub loc: Loc, - pub name: String + pub name: String, + pub exter: bool, } \ No newline at end of file diff --git a/src/constants.rs b/src/constants.rs index ea7d9e2..b87b3f1 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -84,8 +84,10 @@ pub enum KeywordType { ConstantDef, Function, FunctionDef, + FunctionDefInline, FunctionThen, - FunctionDone + FunctionDone, + Inline } #[derive(Debug, Clone, PartialEq)] @@ -198,7 +200,9 @@ impl OpType { KeywordType::FunctionThen => "then", KeywordType::FunctionDone => "done", KeywordType::ConstantDef => "constant Definition (internal)", - KeywordType::FunctionDef => "function definition (internal)" + KeywordType::FunctionDef => "function definition (internal)", + KeywordType::FunctionDefInline => "inline function definition (internal)", + KeywordType::Inline => "inline" } } diff --git a/src/interpret/linux_x86_64/mod.rs b/src/interpret/linux_x86_64/mod.rs index 7ed383f..08f89d4 100644 --- a/src/interpret/linux_x86_64/mod.rs +++ b/src/interpret/linux_x86_64/mod.rs @@ -362,6 +362,7 @@ pub fn run(ops: &[crate::constants::Operator]) -> Result{ KeywordType::While | //* exept this one, this one should just skip over KeywordType::Memory | KeywordType::FunctionDef | + KeywordType::FunctionDefInline | KeywordType::ConstantDef => { //? Disabled since we now pre run the whole program // constants.insert(op.text.clone(), Constant { loc: op.loc.clone(), name: op.text.clone(), value_i: Some(op.value), value_s: None, used: false }); @@ -378,6 +379,7 @@ pub fn run(ops: &[crate::constants::Operator]) -> Result{ KeywordType::FunctionThen => ip += 1, KeywordType::Constant | KeywordType::Function | + KeywordType::Inline | KeywordType::Include => unreachable!(), } } @@ -407,7 +409,8 @@ pub fn pre_run(ops: &[Operator]) -> Defineds { OpType::Keyword(KeywordType::Memory) => { defineds.memories.insert(op.addr.unwrap(), Memory { size: op.value, loc: op.loc.clone(), id: op.addr.unwrap() }); }, - OpType::Keyword(KeywordType::FunctionDef) => { + OpType::Keyword(KeywordType::FunctionDefInline) | + OpType::Keyword(KeywordType::FunctionDef) => { defineds.functions.insert(op.text.clone(), Function { loc: op.loc.clone(), name: op.text.clone(), id: ip }); }, OpType::Keyword(KeywordType::ConstantDef) => { diff --git a/src/parser.rs b/src/parser.rs index cd9d395..0fa7abe 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -44,9 +44,7 @@ pub fn cross_ref(mut program: Vec) -> Result> { program[ip].jmp = program[block_ip].jmp; program[block_ip].jmp = ip + 1; } - OpType::Keyword(KeywordType::FunctionThen) => { - program[ip].typ = OpType::Instruction(InstructionType::Return); - } + OpType::Keyword(KeywordType::Memory | KeywordType::Constant) => (), a => { @@ -208,6 +206,7 @@ pub fn lookup_word>(s: &str, _pos: P) -> OpType { "fn" => OpType::Keyword(KeywordType::Function), "then" => OpType::Keyword(KeywordType::FunctionThen), "done" => OpType::Keyword(KeywordType::FunctionDone), + "inline" => OpType::Keyword(KeywordType::Inline), "return" => OpType::Instruction(InstructionType::Return), "returns" => OpType::Instruction(InstructionType::Returns), "bool" => OpType::Instruction(InstructionType::TypeBool), diff --git a/src/preprocessor.rs b/src/preprocessor.rs index 719bc89..1a68f6b 100644 --- a/src/preprocessor.rs +++ b/src/preprocessor.rs @@ -17,7 +17,9 @@ use crate::parser::lookup_word; #[derive(Debug, Clone)] pub struct Function { pub loc: Loc, - pub name: String + pub name: String, + pub inline: bool, + pub tokens: Option> } #[derive(Debug, Clone)] @@ -62,20 +64,21 @@ impl<'a> Preprocessor<'a> { pub fn preprocess(&mut self) -> Result<&mut Preprocessor<'a>>{ // println!("pre: has do tokens: {:?}", self.program.iter().map(|t| if t.typ == OpType::Keyword(KeywordType::Do) {Some(t)} else {None} ).collect::>>()); + let mut f_inline = false; + let mut f_extern = false; + let mut program: Vec = Vec::new(); let mut rtokens = self.program.clone(); rtokens.reverse(); while !rtokens.is_empty() { - let mut token = rtokens.pop().unwrap(); + let mut op = rtokens.pop().unwrap(); // println!("{token:?}"); - let op_type = token.typ.clone(); - match token.clone() { - - - _ if op_type == OpType::Keyword(KeywordType::Include) => { + let op_type = op.typ.clone(); + match op_type { + OpType::Keyword(KeywordType::Include) => { if rtokens.is_empty() { - lerror!(&token.loc, "Include path not found, expected {} but found nothing", TokenType::String.human()); + lerror!(&op.loc, "Include path not found, expected {} but found nothing", TokenType::String.human()); return Err(eyre!("")); } @@ -127,9 +130,10 @@ impl<'a> Preprocessor<'a> { } - _ if op_type == OpType::Keyword(KeywordType::Memory) => { + + OpType::Keyword(KeywordType::Memory) => { if rtokens.is_empty() { - lerror!(&token.loc, "Memory name not found, expected {} but found nothing", TokenType::String.human()); + lerror!(&op.loc, "Memory name not found, expected {} but found nothing", TokenType::String.human()); return Err(eyre!("")); } @@ -159,19 +163,20 @@ impl<'a> Preprocessor<'a> { if res.len() != 1 { - lerror!(&token.loc, "Expected 1 number, got {:?}", res); + lerror!(&op.loc, "Expected 1 number, got {:?}", res); return Err(eyre!("")); } - token.value = res[0]; - token.addr = Some(self.memories.len()); - program.push(token.clone()); + op.value = res[0]; + op.addr = Some(self.memories.len()); + program.push(op.clone()); - self.memories.insert(name.text, Memory { loc: token.loc, id: self.memories.len() }); + self.memories.insert(name.text, Memory { loc: op.loc, id: self.memories.len() }); } - _ if op_type == OpType::Keyword(KeywordType::Function) => { + + OpType::Keyword(KeywordType::Function) => { if rtokens.is_empty() { - lerror!(&token.loc, "Function name not found, expected {} but found nothing", TokenType::Word.human()); + lerror!(&op.loc, "Function name not found, expected {} but found nothing", TokenType::Word.human()); return Err(eyre!("")); } @@ -206,20 +211,85 @@ impl<'a> Preprocessor<'a> { self.is_word_available(&name, KeywordType::Function)?; - self.functions.insert(name.text.clone(), Function{ - loc: name.loc.clone(), - name: name.text.clone(), - }); + if f_inline { + let mut prog: Vec = Vec::new(); + let mut depth = -1; + while !rtokens.is_empty() { + let op = rtokens.pop().unwrap(); - let mut fn_def = token.clone(); - fn_def.typ = OpType::Keyword(KeywordType::FunctionDef); - fn_def.text = name.text; - // println!("{:?}", token); - program.push(fn_def); + match op.typ.clone() { + OpType::Instruction(i) => { + match i { + InstructionType::TypeAny | + InstructionType::TypeBool | + InstructionType::TypeInt | + InstructionType::TypePtr | + InstructionType::TypeStr | + InstructionType::With | + InstructionType::Returns | + InstructionType::TypeVoid => { + if depth >= 0 { + prog.push(op); + } + }, + _ => prog.push(op) + } + } + OpType::Keyword(k) => { + match k { + KeywordType::Inline | + KeywordType::Include => { + todo!("make error") + }, + KeywordType::FunctionThen => { + if depth >= 0 { + prog.push(op); + } + depth += 1; + }, + KeywordType::FunctionDone => { + if depth == 0 { + break; + } + + depth -= 1; + }, + _ => prog.push(op) + } + } + } + } + let mut pre = self.clone(); + pre.program = prog; + pre.preprocess()?; + prog = pre.get_ops(); + + self.functions.insert(name.text.clone(), Function{ + loc: name.loc.clone(), + name: name.text.clone(), + inline: true, + tokens: Some(prog) + }); + + } else { + self.functions.insert(name.text.clone(), Function{ + loc: name.loc.clone(), + name: name.text.clone(), + inline: false, + tokens: None + }); + + let mut fn_def = op.clone(); + fn_def.typ = OpType::Keyword(KeywordType::FunctionDef); + fn_def.text = name.text; + // println!("{:?}", token); + program.push(fn_def); + } } - _ if op_type == OpType::Keyword(KeywordType::Constant) => { + + OpType::Keyword(KeywordType::Constant) => { if rtokens.is_empty() { - lerror!(&token.loc, "Constant name not found, expected {} but found nothing", TokenType::Word.human()); + lerror!(&op.loc, "Constant name not found, expected {} but found nothing", TokenType::Word.human()); return Err(eyre!("")); } // println!("{token:?}"); @@ -263,7 +333,7 @@ impl<'a> Preprocessor<'a> { // println!("{:?}", self.constants); - let mut const_def = token.clone(); + let mut const_def = op.clone(); const_def.typ = OpType::Keyword(KeywordType::ConstantDef); const_def.text = name.text; @@ -271,14 +341,14 @@ impl<'a> Preprocessor<'a> { if item.tok_typ == TokenType::Int { const_def.value = item.value; } else { - lerror!(&token.loc, "For now only {:?} is allowed in constants", TokenType::Int); + lerror!(&op.loc, "For now only {:?} is allowed in constants", TokenType::Int); return Err(eyre!("")); } let posibly_end = rtokens.pop(); // println!("end: {posibly_end:?}"); if posibly_end.is_none() || posibly_end.unwrap().typ != OpType::Keyword(KeywordType::End) { - lerror!(&token.loc, "Constant was not closed with an 'end' instruction, expected 'end' but found nothing"); + lerror!(&op.loc, "Constant was not closed with an 'end' instruction, expected 'end' but found nothing"); return Err(eyre!("")); } // token.value = @@ -286,9 +356,17 @@ impl<'a> Preprocessor<'a> { program.push(const_def); } + OpType::Keyword(KeywordType::Inline) => { + if f_inline { + lerror!(&op.loc, "Function is already marked as inline, remove this inline Keyword"); + return Err(eyre!("")); + } else { + f_inline = true; + } + } + _ => { - - program.push(token); + program.push(op); } } } @@ -334,15 +412,18 @@ impl<'a> Preprocessor<'a> { if op.tok_typ == TokenType::Word { match op_type { OpType::Instruction(InstructionType::None) => { - let m = self.functions.get(&op.text); + let m = self.functions.get(&op.text.clone().replace('(', "__OP_PAREN__").replace(')', "__CL_PAREN__")); let mem = self.memories.get(&op.text); - let cons = self.constants.get(&op.text); + let cons = self.constants.get(&op.text.clone().replace('(', "__OP_PAREN__").replace(')', "__CL_PAREN__")); if let Some(m) = m { - // println!("------ FOUND FUNCTION {} -----------", m.name); - let mut t = op.clone(); - t.typ = OpType::Instruction(InstructionType::FnCall); - t.text = m.name.clone(); - program.push(t.clone()); + if m.inline { + program.append(&mut m.tokens.clone().unwrap()); + } else { + let mut t = op.clone(); + t.typ = OpType::Instruction(InstructionType::FnCall); + t.text = m.name.clone(); + program.push(t.clone()); + } // println!("##### {:?}", t); } else if let Some(mem) = mem { diff --git a/src/typechecker.rs b/src/typechecker.rs index a32c653..2716ded 100644 --- a/src/typechecker.rs +++ b/src/typechecker.rs @@ -62,6 +62,7 @@ pub fn typecheck(ops: Vec, args: &Args, init_types: Option> stack_pop(&mut stack, &op, &[Types::Bool])?; }, + KeywordType::FunctionDefInline | KeywordType::FunctionDef => { let name = op.text.clone(); @@ -177,6 +178,7 @@ pub fn typecheck(ops: Vec, args: &Args, init_types: Option> KeywordType::FunctionThen | KeywordType::FunctionDone | KeywordType::Function => unreachable!(), + KeywordType::Inline => (), } }, OpType::Instruction(instruction) => { diff --git a/test.mcl b/test.mcl index c2cfa73..c6336ff 100644 --- a/test.mcl +++ b/test.mcl @@ -1,5 +1,7 @@ -include "std.mcl" +// include "std.mcl" fn main with void returns void then - "hi\n" puts -done \ No newline at end of file + 69 test +done + +inline fn test with void returns void then _dbg_print done \ No newline at end of file From 2d7f302d427f9f4e5b11a50b63b5236716a126dd Mon Sep 17 00:00:00 2001 From: MCorange Date: Thu, 13 Apr 2023 04:57:28 +0300 Subject: [PATCH 3/4] Added function features Added features: - Exported functions (very experimental and disabled by default with hardcoded toggle) - Inline functions --- src/compile/linux_x86_64.rs | 94 ++++++++++++++++++++++++++----- src/config.rs | 8 ++- src/constants.rs | 27 +++++---- src/interpret/linux_x86_64/mod.rs | 10 ++-- src/main.rs | 3 + src/parser.rs | 2 +- src/preprocessor.rs | 65 ++++++++++++++++++++- src/typechecker.rs | 20 ++++--- test.mcl | 13 +++-- 9 files changed, 199 insertions(+), 43 deletions(-) diff --git a/src/compile/linux_x86_64.rs b/src/compile/linux_x86_64.rs index 79418af..dc12d42 100644 --- a/src/compile/linux_x86_64.rs +++ b/src/compile/linux_x86_64.rs @@ -1,5 +1,5 @@ use std::{fs, path::PathBuf, io::{Write, BufWriter}, collections::HashMap}; -use crate::{constants::{Operator, OpType, KeywordType}, Args}; +use crate::{constants::{Operator, OpType, KeywordType}, Args, warn, error, lerror}; use color_eyre::Result; use crate::compile::commands::linux_x86_64_compile_and_link; use crate::constants::InstructionType; @@ -25,6 +25,7 @@ pub fn compile(tokens: &[Operator], args: &Args) -> Result{ of_o.set_extension("o"); of_a.set_extension("nasm"); + let mut should_push_ret = false; let file = fs::File::create(&of_a)?; let mut writer = BufWriter::new(&file); @@ -71,11 +72,14 @@ pub fn compile(tokens: &[Operator], args: &Args) -> Result{ writeln!(writer, " add rsp, 40")?; writeln!(writer, " ret")?; - writeln!(writer, "global _start")?; - writeln!(writer, "_start:")?; - writeln!(writer, " lea rbp, [rel ret_stack]")?; - writeln!(writer, " call main")?; - writeln!(writer, " jmp end")?; + if crate::config::ENABLE_EXPORTED_FUNCTIONS && !args.lib_mode { + writeln!(writer, "global _start")?; + writeln!(writer, "_start:")?; + writeln!(writer, " lea rbp, [rel ret_stack]")?; + writeln!(writer, " call main")?; + writeln!(writer, " jmp end")?; + + } let mut ti = 0; @@ -416,6 +420,12 @@ pub fn compile(tokens: &[Operator], args: &Args) -> Result{ ti += 1; }, InstructionType::Return => { + + if crate::config::ENABLE_EXPORTED_FUNCTIONS && should_push_ret { + writeln!(writer, " pop rdx")?; + should_push_ret = false; + } + writeln!(writer, " sub rbp, 8")?; writeln!(writer, " mov rbx, qword [rbp]")?; writeln!(writer, " push rbx")?; @@ -430,7 +440,6 @@ pub fn compile(tokens: &[Operator], args: &Args) -> Result{ InstructionType::TypePtr | InstructionType::TypeInt | InstructionType::TypeVoid | - InstructionType::TypeStr | InstructionType::TypeAny | InstructionType::Returns | InstructionType::With => { @@ -501,6 +510,12 @@ pub fn compile(tokens: &[Operator], args: &Args) -> Result{ ti += 1; }, KeywordType::FunctionDone => { + + if crate::config::ENABLE_EXPORTED_FUNCTIONS && should_push_ret { + writeln!(writer, " pop rdx")?; + should_push_ret = false; + } + writeln!(writer, " sub rbp, 8")?; writeln!(writer, " mov rbx, qword [rbp]")?; writeln!(writer, " push rbx")?; @@ -510,18 +525,71 @@ pub fn compile(tokens: &[Operator], args: &Args) -> Result{ KeywordType::FunctionThen => ti += 1, KeywordType::Function | KeywordType::Include | - KeywordType::FunctionDefInline | + KeywordType::Inline | + KeywordType::Export | KeywordType::Constant => unreachable!(), - KeywordType::Inline => todo!(), + KeywordType::FunctionDefExported => { + + if !crate::config::ENABLE_EXPORTED_FUNCTIONS { + lerror!(&token.loc, "Experimental feature 'exported functions' is not enabled"); + return Err(eyre!("")); + } + + writeln!(writer, "global {}", token.text)?; + writeln!(writer, "{}:", token.text)?; + + writeln!(writer, " pop rbx")?; + writeln!(writer, " mov qword [rbp], rbx")?; + writeln!(writer, " add rbp, 8")?; + warn!("External functions are highly experimental and should be treated as such"); + if token.types.0 == 0 { + writeln!(writer, " ; no arguments")?; + } else { + if token.types.0 >= 1 { + writeln!(writer, " push rdi")?; + } + if token.types.0 >= 2 { + writeln!(writer, " push rsi")?; + } + if token.types.0 >= 3 { + writeln!(writer, " push rdx")?; + } + if token.types.0 >= 4 { + writeln!(writer, " push rcx")?; + } + if token.types.0 >= 5 { + writeln!(writer, " push r8")?; + } + if token.types.0 >= 6 { + writeln!(writer, " push r9")?; + } + if token.types.0 >= 7 { + lerror!(&token.loc, "More than 6 arguments in an external function is not supported"); + return Err(eyre!("")); + } + } + + if token.types.1 == 1 { + should_push_ret = true; + } else if token.types.1 > 1 { + lerror!(&token.loc, "More than 1 return arguments in an external function is not supported"); + return Err(eyre!("")); + } + + functions.push(Function { loc: token.loc.clone(), name: token.text.clone(), exter: false}); + ti += 1; + }, } } } } writeln!(writer, "addr_{ti}:")?; - writeln!(writer, "end:")?; - writeln!(writer, " mov rax, 60")?; - writeln!(writer, " mov rdi, 0")?; - writeln!(writer, " syscall")?; + if crate::config::ENABLE_EXPORTED_FUNCTIONS && !args.lib_mode { + writeln!(writer, "end:")?; + writeln!(writer, " mov rax, 60")?; + writeln!(writer, " mov rdi, 0")?; + writeln!(writer, " syscall")?; + } writeln!(writer, "segment .data")?; for (i, s) in strings.iter().enumerate() { let s_chars = s.chars().map(|c| (c as u32).to_string()).collect::>(); diff --git a/src/config.rs b/src/config.rs index 55ff8b4..c6c1b77 100644 --- a/src/config.rs +++ b/src/config.rs @@ -17,4 +17,10 @@ pub const DEFAULT_INCLUDES: [&str;1] = [ * if you have buffer overflow consider increasing these */ pub const MEM_SZ: usize = 640 * 1000; // 4kb -pub const STRING_SZ: usize = 640 * 1000; // 4kb \ No newline at end of file +pub const STRING_SZ: usize = 640 * 1000; // 4kb + + +/** + * Experimental options + */ +pub const ENABLE_EXPORTED_FUNCTIONS: bool = false; \ No newline at end of file diff --git a/src/constants.rs b/src/constants.rs index b87b3f1..9027aa6 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -58,7 +58,7 @@ pub enum InstructionType { TypePtr, TypeInt, TypeVoid, - TypeStr, + // TypeStr, TypeAny, Returns, With, @@ -84,10 +84,11 @@ pub enum KeywordType { ConstantDef, Function, FunctionDef, - FunctionDefInline, + FunctionDefExported, FunctionThen, FunctionDone, - Inline + Inline, + Export } #[derive(Debug, Clone, PartialEq)] @@ -104,7 +105,8 @@ pub struct Operator{ pub text: String, //? only used for OpType::PushStr pub addr: Option, //? only used for OpType::PushStr pub jmp: usize, - pub loc: Loc + pub loc: Loc, + pub types: (usize, usize) } impl Operator { @@ -117,13 +119,19 @@ impl Operator { text, loc: (file, row, col), tok_typ, + types: (0, 0) } } - pub fn set_addr(mut self, addr: usize) -> Self { + pub fn set_addr(&mut self, addr: usize) -> Self { self.addr = Some(addr); - self + (*self).clone() } + // pub fn set_types(&mut self, args: usize, rets: usize) -> Self { + // self.types = (args, rets); + // (*self).clone() + // } + } impl OpType { @@ -180,7 +188,6 @@ impl OpType { InstructionType::TypePtr => "ptr", InstructionType::TypeInt => "int", InstructionType::TypeVoid => "void", - InstructionType::TypeStr => "str", InstructionType::Returns => "returns", InstructionType::With => "with", InstructionType::TypeAny => "any", @@ -201,8 +208,9 @@ impl OpType { KeywordType::FunctionDone => "done", KeywordType::ConstantDef => "constant Definition (internal)", KeywordType::FunctionDef => "function definition (internal)", - KeywordType::FunctionDefInline => "inline function definition (internal)", - KeywordType::Inline => "inline" + KeywordType::FunctionDefExported => "extern function definition (internal)", + KeywordType::Inline => "inline", + KeywordType::Export => "export", } } @@ -259,7 +267,6 @@ pub enum Types { Ptr, Int, Void, - Str, Any // U8, // U16, diff --git a/src/interpret/linux_x86_64/mod.rs b/src/interpret/linux_x86_64/mod.rs index 08f89d4..62c686e 100644 --- a/src/interpret/linux_x86_64/mod.rs +++ b/src/interpret/linux_x86_64/mod.rs @@ -328,7 +328,6 @@ pub fn run(ops: &[crate::constants::Operator]) -> Result{ InstructionType::TypePtr | InstructionType::TypeInt | InstructionType::TypeVoid | - InstructionType::TypeStr | InstructionType::TypeAny | InstructionType::Returns | InstructionType::With => ip += 1, @@ -362,7 +361,7 @@ pub fn run(ops: &[crate::constants::Operator]) -> Result{ KeywordType::While | //* exept this one, this one should just skip over KeywordType::Memory | KeywordType::FunctionDef | - KeywordType::FunctionDefInline | + KeywordType::FunctionDefExported | KeywordType::ConstantDef => { //? Disabled since we now pre run the whole program // constants.insert(op.text.clone(), Constant { loc: op.loc.clone(), name: op.text.clone(), value_i: Some(op.value), value_s: None, used: false }); @@ -380,6 +379,7 @@ pub fn run(ops: &[crate::constants::Operator]) -> Result{ KeywordType::Constant | KeywordType::Function | KeywordType::Inline | + KeywordType::Export | KeywordType::Include => unreachable!(), } } @@ -409,8 +409,10 @@ pub fn pre_run(ops: &[Operator]) -> Defineds { OpType::Keyword(KeywordType::Memory) => { defineds.memories.insert(op.addr.unwrap(), Memory { size: op.value, loc: op.loc.clone(), id: op.addr.unwrap() }); }, - OpType::Keyword(KeywordType::FunctionDefInline) | - OpType::Keyword(KeywordType::FunctionDef) => { + OpType::Keyword(KeywordType::FunctionDefExported) => { + + } + OpType::Keyword(KeywordType::FunctionDef) => { defineds.functions.insert(op.text.clone(), Function { loc: op.loc.clone(), name: op.text.clone(), id: ip }); }, OpType::Keyword(KeywordType::ConstantDef) => { diff --git a/src/main.rs b/src/main.rs index 5aa2839..9de7469 100644 --- a/src/main.rs +++ b/src/main.rs @@ -57,6 +57,9 @@ pub struct Args { #[arg(long, short='O', default_value_t=String::from("0"))] optimisation: String, + // disables the main function + #[arg(long="lib")] + lib_mode: bool //#[arg(long, short='F')] //features: Vec, diff --git a/src/parser.rs b/src/parser.rs index 0fa7abe..61ffe43 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -207,6 +207,7 @@ pub fn lookup_word>(s: &str, _pos: P) -> OpType { "then" => OpType::Keyword(KeywordType::FunctionThen), "done" => OpType::Keyword(KeywordType::FunctionDone), "inline" => OpType::Keyword(KeywordType::Inline), + "export" => OpType::Keyword(KeywordType::Export), "return" => OpType::Instruction(InstructionType::Return), "returns" => OpType::Instruction(InstructionType::Returns), "bool" => OpType::Instruction(InstructionType::TypeBool), @@ -214,7 +215,6 @@ pub fn lookup_word>(s: &str, _pos: P) -> OpType { "ptr" => OpType::Instruction(InstructionType::TypePtr), "void" => OpType::Instruction(InstructionType::TypeVoid), "any" => OpType::Instruction(InstructionType::TypeAny), - "str" => OpType::Instruction(InstructionType::TypeStr), "with" => OpType::Instruction(InstructionType::With), _ => OpType::Instruction(InstructionType::None) } diff --git a/src/preprocessor.rs b/src/preprocessor.rs index 1a68f6b..360e47b 100644 --- a/src/preprocessor.rs +++ b/src/preprocessor.rs @@ -212,6 +212,7 @@ impl<'a> Preprocessor<'a> { if f_inline { + f_inline = false; let mut prog: Vec = Vec::new(); let mut depth = -1; while !rtokens.is_empty() { @@ -224,7 +225,6 @@ impl<'a> Preprocessor<'a> { InstructionType::TypeBool | InstructionType::TypeInt | InstructionType::TypePtr | - InstructionType::TypeStr | InstructionType::With | InstructionType::Returns | InstructionType::TypeVoid => { @@ -271,7 +271,52 @@ impl<'a> Preprocessor<'a> { tokens: Some(prog) }); + } else if f_extern { + f_extern = false; + self.functions.insert(name.text.clone(), Function{ + loc: name.loc.clone(), + name: name.text.clone(), + inline: false, + tokens: None + }); + let mut a: Vec = Vec::new(); + let mut fn_def = op.clone(); + a.push(rtokens.pop().unwrap()); + let mut ret = false; + while !rtokens.is_empty() { + let op = rtokens.pop().unwrap(); + // println!("{:?}",op); + a.push(op.clone()); + if op.typ == OpType::Instruction(InstructionType::Returns) { + ret = true; + } + + if op.typ == OpType::Keyword(KeywordType::FunctionThen) { + break; + } + + if op.typ == OpType::Instruction(InstructionType::TypeBool) || + op.typ == OpType::Instruction(InstructionType::TypeInt) || + op.typ == OpType::Instruction(InstructionType::TypePtr) { + + if ret { + fn_def.types.1 += 1; + } else { + fn_def.types.0 += 1; + } + } + } + + fn_def.typ = OpType::Keyword(KeywordType::FunctionDefExported); + fn_def.text = name.text; + // fn_def.set_types(args, rets); + // println!("{:?}", fn_def.types); + program.push(fn_def); + program.append(&mut a); + + } else { + self.functions.insert(name.text.clone(), Function{ loc: name.loc.clone(), name: name.text.clone(), @@ -357,7 +402,10 @@ impl<'a> Preprocessor<'a> { } OpType::Keyword(KeywordType::Inline) => { - if f_inline { + if f_extern { + lerror!(&op.loc, "Function is already marked as extern, function cannot be inline and extern at the same time"); + return Err(eyre!("")); + } else if f_inline { lerror!(&op.loc, "Function is already marked as inline, remove this inline Keyword"); return Err(eyre!("")); } else { @@ -365,6 +413,18 @@ impl<'a> Preprocessor<'a> { } } + OpType::Keyword(KeywordType::Export) => { + if f_inline { + lerror!(&op.loc, "Function is already marked as inline, function cannot be inline and extern at the same time"); + return Err(eyre!("")); + } else if f_extern { + lerror!(&op.loc, "Function is already marked as extern, remove this extern Keyword"); + return Err(eyre!("")); + } else { + f_extern = true; + } + } + _ => { program.push(op); } @@ -381,6 +441,7 @@ impl<'a> Preprocessor<'a> { f.typ != OpType::Instruction(InstructionType::FnCall) && f.typ != OpType::Instruction(InstructionType::MemUse) && f.typ != OpType::Keyword(KeywordType::FunctionDef) && + f.typ != OpType::Keyword(KeywordType::FunctionDefExported) && f.typ != OpType::Keyword(KeywordType::ConstantDef) && f.typ != OpType::Instruction(InstructionType::ConstUse) { lookup_word(&f.text, &f.loc) diff --git a/src/typechecker.rs b/src/typechecker.rs index 2716ded..1d256b0 100644 --- a/src/typechecker.rs +++ b/src/typechecker.rs @@ -62,10 +62,10 @@ pub fn typecheck(ops: Vec, args: &Args, init_types: Option> stack_pop(&mut stack, &op, &[Types::Bool])?; }, - KeywordType::FunctionDefInline | + KeywordType::FunctionDefExported | KeywordType::FunctionDef => { let name = op.text.clone(); - + // println!("{:?}", name); if let Some(p) = rtokens.pop() { if p.typ != OpType::Instruction(InstructionType::With){ lerror!(&op.loc, "Expected {:?}, got {:?}", OpType::Instruction(InstructionType::With), p.typ); @@ -106,8 +106,6 @@ pub fn typecheck(ops: Vec, args: &Args, init_types: Option> continue; } Types::Void - } else if op.typ == OpType::Instruction(InstructionType::TypeStr) { - Types::Str } else if op.typ == OpType::Instruction(InstructionType::TypeAny) { Types::Any } else { @@ -177,8 +175,12 @@ pub fn typecheck(ops: Vec, args: &Args, init_types: Option> }, KeywordType::FunctionThen | KeywordType::FunctionDone | - KeywordType::Function => unreachable!(), - KeywordType::Inline => (), + KeywordType::Inline | + KeywordType::Export | + KeywordType::Function => { + println!("{:?}", op); + unreachable!() + }, } }, OpType::Instruction(instruction) => { @@ -332,7 +334,10 @@ pub fn typecheck(ops: Vec, args: &Args, init_types: Option> InstructionType::FnCall => { stack_snapshots.push(stack.clone()); - let f = functions.get(&op.text).unwrap().clone(); + let f = if let Some(f) = functions.get(&op.text) {f} else { + lerror!(&op.loc, "Could not find function {}", op.text); + return Err(eyre!("")); + }; // in_function = (op.text.clone(), f.clone(), op.loc.clone()); @@ -362,7 +367,6 @@ pub fn typecheck(ops: Vec, args: &Args, init_types: Option> InstructionType::TypeInt | InstructionType::TypeVoid | InstructionType::TypeAny | - InstructionType::TypeStr | InstructionType::Returns | InstructionType::With => (), InstructionType::ConstUse => { diff --git a/test.mcl b/test.mcl index c6336ff..4e424cf 100644 --- a/test.mcl +++ b/test.mcl @@ -1,7 +1,12 @@ // include "std.mcl" - -fn main with void returns void then - 69 test +fn mcl_print with int ptr returns void then + 1 1 syscall3 drop done -inline fn test with void returns void then _dbg_print done \ No newline at end of file +fn mcl_dump with int returns void then + _dbg_print +done + +fn main with void returns void then + "hi\n" mcl_print +done From 30ffb0b07a0d3a985880b02f76ba023cda6748d9 Mon Sep 17 00:00:00 2001 From: MCorange Date: Thu, 13 Apr 2023 04:59:08 +0300 Subject: [PATCH 4/4] Removed warning and disabled dev mode --- src/compile/linux_x86_64.rs | 2 +- src/config.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/compile/linux_x86_64.rs b/src/compile/linux_x86_64.rs index dc12d42..572caf7 100644 --- a/src/compile/linux_x86_64.rs +++ b/src/compile/linux_x86_64.rs @@ -1,5 +1,5 @@ use std::{fs, path::PathBuf, io::{Write, BufWriter}, collections::HashMap}; -use crate::{constants::{Operator, OpType, KeywordType}, Args, warn, error, lerror}; +use crate::{constants::{Operator, OpType, KeywordType}, Args, warn, lerror}; use color_eyre::Result; use crate::compile::commands::linux_x86_64_compile_and_link; use crate::constants::InstructionType; diff --git a/src/config.rs b/src/config.rs index c6c1b77..214c4be 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,7 +1,7 @@ /** * Prints out extra information */ -pub const DEV_MODE: bool = true; +pub const DEV_MODE: bool = false; pub const DEFAULT_OUT_FILE: &str = "a.out"; pub const DEFAULT_INCLUDES: [&str;1] = [