From 24029b09d6c22a4e18f9e96c7f8083f62d8e7ee5 Mon Sep 17 00:00:00 2001 From: Young-Flash Date: Thu, 2 Jan 2025 17:29:57 +0800 Subject: [PATCH 1/2] bump Yoorkin/ArgParser to 0.1.7 --- moon.mod.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/moon.mod.json b/moon.mod.json index 2c31fa6..ed7a441 100644 --- a/moon.mod.json +++ b/moon.mod.json @@ -3,7 +3,7 @@ "version": "0.1.0", "deps": { "moonbitlang/x": "0.4.15", - "Yoorkin/ArgParser": "0.1.6" + "Yoorkin/ArgParser": "0.1.7" }, "readme": "README.md", "license": "BSD-3-Clause", From 7b65e388259289084ed8a8e7ceede58bb8f5ee63 Mon Sep 17 00:00:00 2001 From: Young-Flash Date: Thu, 2 Jan 2025 17:30:12 +0800 Subject: [PATCH 2/2] moon info --- src/bin/bin.mbti | 8 +++- src/closure/closure.mbti | 76 ++++++++++++++---------------- src/closure_eval/closure_eval.mbti | 39 +++++++++++++++ src/knf/knf.mbti | 38 +++++++-------- src/knf_eval/knf_eval.mbti | 7 +-- src/lex/lex.mbti | 2 - src/minimbt.mbti | 72 +++++++++++++--------------- src/parser/parser.mbti | 2 - src/riscv/riscv.mbti | 53 +++++++++------------ src/typing/typing.mbti | 2 - src/util/util.mbti | 6 +-- 11 files changed, 158 insertions(+), 147 deletions(-) create mode 100644 src/closure_eval/closure_eval.mbti diff --git a/src/bin/bin.mbti b/src/bin/bin.mbti index 76ba349..ac61267 100644 --- a/src/bin/bin.mbti +++ b/src/bin/bin.mbti @@ -3,10 +3,14 @@ package moonbitlang/minimbt/bin // Values // Types and methods +type CompileStatus + +type Stages +impl Compare for Stages +impl Eq for Stages +impl Show for Stages // Type aliases // Traits -// Extension Methods - diff --git a/src/closure/closure.mbti b/src/closure/closure.mbti index 2d4561d..84e8446 100644 --- a/src/closure/closure.mbti +++ b/src/closure/closure.mbti @@ -7,15 +7,17 @@ alias @moonbitlang/minimbt/knf as @knf fn knf_program_to_closure(@knf.Knf, Map[String, @minimbt.Type]) -> Program // Types and methods -pub struct Closure { - pub name : Label - pub actual_free_vars : Array[@minimbt.Name] +pub(all) struct Closure { + name : Label + actual_free_vars : Array[@minimbt.Name] } impl Closure { - to_string(Self) -> String + from_json(Json) -> Self! + to_json(Self) -> Json } +impl Show for Closure -pub enum Expr { +pub(all) enum Expr { Unit Int(Int) Double(Double) @@ -31,39 +33,42 @@ pub enum Expr { FDiv(@minimbt.Name, @minimbt.Name) IfEq(@minimbt.Name, @minimbt.Name, Expr, Expr) IfLe(@minimbt.Name, @minimbt.Name, Expr, Expr) - Let(Tuple[@minimbt.Name, LowType], Expr, Expr) + Let((@minimbt.Name, LowType), Expr, Expr) Var(@minimbt.Name) - MakeClosure(Tuple[@minimbt.Name, LowType], Closure, Expr) + MakeClosure((@minimbt.Name, LowType), Closure, Expr) CallClosure(@minimbt.Name, Array[@minimbt.Name]) CallDirect(Label, Array[@minimbt.Name]) MakeTuple(Array[@minimbt.Name]) - LetTuple(Array[Tuple[@minimbt.Name, LowType]], @minimbt.Name, Expr) + LetTuple(Array[(@minimbt.Name, LowType)], @minimbt.Name, Expr) ArrayGet(@minimbt.Name, @minimbt.Name) ArrayPut(@minimbt.Name, @minimbt.Name, @minimbt.Name) ExternalArray(Label) } impl Expr { - to_string(Self) -> String + from_json(Json) -> Self! + to_json(Self) -> Json } +impl Show for Expr -pub struct FuncDef { - pub name : Label - pub old_name : @minimbt.Name - pub ty : LowType - pub args : Array[Tuple[@minimbt.Name, LowType]] - pub formal_free_vars : Array[Tuple[@minimbt.Name, LowType]] - pub body : Expr +pub(all) struct FuncDef { + name : Label + old_name : @minimbt.Name + is_closure : Bool + ty : LowType + args : Array[(@minimbt.Name, LowType)] + formal_free_vars : Array[(@minimbt.Name, LowType)] + body : Expr } impl FuncDef { - to_string(Self) -> String + from_json(Json) -> Self! + to_json(Self) -> Json } +impl Show for FuncDef -pub type Label String -impl Label { - to_string(Self) -> String -} +pub(all) type Label String +impl Show for Label -pub enum LowType { +pub(all) enum LowType { Unit Bool Int @@ -75,19 +80,23 @@ pub enum LowType { Ptr } impl LowType { + from_json(Json) -> Self! is_float_like(Self) -> Bool is_ptr_like(Self) -> Bool size_of(Self, Int) -> Int - to_string(Self) -> String + to_json(Self) -> Json } +impl Show for LowType -pub struct Program { - pub fundefs : Array[FuncDef] - pub body : Expr +pub(all) struct Program { + fundefs : Array[FuncDef] + body : Expr } impl Program { - to_string(Self) -> String + from_json(Json) -> Self! + to_json(Self) -> Json } +impl Show for Program // Type aliases pub typealias LocalEnv = @moonbitlang/core/immut/hashmap.T[@minimbt.Name, LowType] @@ -98,16 +107,3 @@ pub typealias Type = @minimbt.Type // Traits -// Extension Methods -impl Show for Closure - -impl Show for Expr - -impl Show for FuncDef - -impl Show for Label - -impl Show for LowType - -impl Show for Program - diff --git a/src/closure_eval/closure_eval.mbti b/src/closure_eval/closure_eval.mbti new file mode 100644 index 0000000..5cfef18 --- /dev/null +++ b/src/closure_eval/closure_eval.mbti @@ -0,0 +1,39 @@ +package moonbitlang/minimbt/closure_eval + +alias @moonbitlang/core/immut/hashmap as @hashmap +alias @moonbitlang/minimbt as @minimbt +alias @moonbitlang/minimbt/closure as @closure + +// Values + +// Types and methods +type ClosureInterpreter +impl ClosureInterpreter { + add_extern_fn(Self, String, (Array[Value]) -> Value) -> Unit + eval(Self, @hashmap.T[@minimbt.Name, Value], @closure.Expr) -> Value!Failure + eval_fn(Self, @closure.FuncDef, Array[Value], Array[Value]) -> Value!Failure + eval_full(Self, @closure.Program) -> Value!Failure + new() -> Self +} + +pub(all) enum Value { + Unit + Int(Int) + Double(Double) + Tuple(Array[Value]) + Array(Array[Value]) + ExternFn(String) + Closure(@closure.FuncDef, Array[Value]) +} +impl Value { + op_equal(Self, Self) -> Bool +} +impl Show for Value + +// Type aliases +pub typealias InterpreterLocalVars = @hashmap.T[@minimbt.Name, Value] + +pub typealias Name = @minimbt.Name + +// Traits + diff --git a/src/knf/knf.mbti b/src/knf/knf.mbti index e981dd6..5b68050 100644 --- a/src/knf/knf.mbti +++ b/src/knf/knf.mbti @@ -4,20 +4,23 @@ alias @moonbitlang/core/immut/hashmap as @hashmap alias @moonbitlang/minimbt as @minimbt // Values +fn knf_max_counter(Knf) -> Int // Types and methods -pub struct FuncDef { - pub name : @minimbt.Name - pub ty : @minimbt.Type - pub args : Array[Tuple[@minimbt.Name, @minimbt.Type]] - pub body : Knf +pub(all) struct FuncDef { + name : @minimbt.Name + ty : @minimbt.Type + args : Array[(@minimbt.Name, @minimbt.Type)] + body : Knf } impl FuncDef { - op_equal(Self, Self) -> Bool - to_string(Self) -> String + from_json(Json) -> Self! + to_json(Self) -> Json } +impl Eq for FuncDef +impl Show for FuncDef -pub enum Knf { +pub(all) enum Knf { Unit Int(Int) Double(Double) @@ -33,25 +36,28 @@ pub enum Knf { FDiv(@minimbt.Name, @minimbt.Name) IfEq(@minimbt.Name, @minimbt.Name, Knf, Knf) IfLe(@minimbt.Name, @minimbt.Name, Knf, Knf) - Let(Tuple[@minimbt.Name, @minimbt.Type], Knf, Knf) + Let((@minimbt.Name, @minimbt.Type), Knf, Knf) Var(@minimbt.Name) LetRec(FuncDef, Knf) Apply(@minimbt.Name, Array[@minimbt.Name]) Tuple(Array[@minimbt.Name]) - LetTuple(Array[Tuple[@minimbt.Name, @minimbt.Type]], @minimbt.Name, Knf) + LetTuple(Array[(@minimbt.Name, @minimbt.Type)], @minimbt.Name, Knf) Get(@minimbt.Name, @minimbt.Name) Put(@minimbt.Name, @minimbt.Name, @minimbt.Name) ExternalArray(@minimbt.Name) ExternalFunctionApplication(String, Array[@minimbt.Name]) } impl Knf { - op_equal(Self, Self) -> Bool + from_json(Json) -> Self! + to_json(Self) -> Json to_pretty_print(Self) -> PrettyKnf - to_string(Self) -> String } +impl Eq for Knf +impl Show for Knf type KnfEnv impl KnfEnv { + init_counter_from_existing(Self, Knf) -> Unit new(@hashmap.T[String, @minimbt.Type]) -> Self opt_pass(Self, Knf) -> Knf syntax_preprocess(Self, @minimbt.Syntax) -> @minimbt.Syntax @@ -59,6 +65,7 @@ impl KnfEnv { } type PrettyKnf +impl Show for PrettyKnf // Type aliases pub typealias LocalEnv = @hashmap.T[String, @minimbt.Type] @@ -69,10 +76,3 @@ pub typealias Type = @minimbt.Type // Traits -// Extension Methods -impl Show for FuncDef - -impl Show for Knf - -impl Show for PrettyKnf - diff --git a/src/knf_eval/knf_eval.mbti b/src/knf_eval/knf_eval.mbti index 3792996..e03c28a 100644 --- a/src/knf_eval/knf_eval.mbti +++ b/src/knf_eval/knf_eval.mbti @@ -15,7 +15,7 @@ impl KnfInterpreter { new() -> Self } -pub enum Value { +pub(all) enum Value { Unit Int(Int) Double(Double) @@ -26,8 +26,8 @@ pub enum Value { } impl Value { op_equal(Self, Self) -> Bool - to_string(Self) -> String } +impl Show for Value // Type aliases pub typealias InterpreterLocalVars = @hashmap.T[@minimbt.Name, Value] @@ -36,6 +36,3 @@ pub typealias Name = @minimbt.Name // Traits -// Extension Methods -impl Show for Value - diff --git a/src/lex/lex.mbti b/src/lex/lex.mbti index 4a0cc95..9143900 100644 --- a/src/lex/lex.mbti +++ b/src/lex/lex.mbti @@ -8,5 +8,3 @@ package moonbitlang/minimbt/lex // Traits -// Extension Methods - diff --git a/src/minimbt.mbti b/src/minimbt.mbti index bb9aa68..0b98724 100644 --- a/src/minimbt.mbti +++ b/src/minimbt.mbti @@ -3,52 +3,57 @@ package moonbitlang/minimbt // Values // Types and methods -pub struct Fundef { - pub name : Tuple[String, Type] - pub args : Array[Tuple[String, Type]] - pub body : Syntax +pub(all) struct Fundef { + name : (String, Type) + args : Array[(String, Type)] + body : Syntax } impl Fundef { from_json(Json) -> Self! to_json(Self) -> Json - to_string(Self) -> String } +impl Show for Fundef -pub enum Kind { +pub(all) enum Kind { Int Double } -impl Kind { - op_equal(Self, Self) -> Bool - to_string(Self) -> String -} +impl Eq for Kind +impl Show for Kind -pub struct Name { - pub name : String? - pub slot : Int +pub(all) struct Name { + name : String? + slot : Int } impl Name { - compare(Self, Self) -> Int - hash_combine(Self, Hasher) -> Unit + from_json(Json) -> Self! + from_string(String) -> Self!NameFromStringError name_and_slot(String, Int) -> Self name_only(String) -> Self - op_equal(Self, Self) -> Bool slot_only(Int) -> Self + to_json(Self) -> Json to_string(Self) -> String } +impl Compare for Name +impl Eq for Name +impl Hash for Name +impl Show for Name + +pub(all) type! NameFromStringError { + InvalidSlotNumber(String, String) +} +impl Show for NameFromStringError -pub enum Op { +pub(all) enum Op { Add Sub Mul Div } -impl Op { - op_equal(Self, Self) -> Bool - to_string(Self) -> String -} +impl Eq for Op +impl Show for Op -pub enum Syntax { +pub(all) enum Syntax { Unit Bool(Bool) Int(Int) @@ -64,18 +69,18 @@ pub enum Syntax { Prim(Syntax, Syntax, Op, Kind?) Eq(Syntax, Syntax) LE(Syntax, Syntax) - Let(Tuple[String, Type], Syntax, Syntax) + Let((String, Type), Syntax, Syntax) LetRec(Fundef, Syntax) - LetTuple(Array[Tuple[String, Type]], Syntax, Syntax) + LetTuple(Array[(String, Type)], Syntax, Syntax) Put(Syntax, Syntax, Syntax) } impl Syntax { from_json(Json) -> Self! to_json(Self) -> Json - to_string(Self) -> String } +impl Show for Syntax -pub enum Type { +pub(all) enum Type { Unit Bool Int @@ -93,23 +98,10 @@ impl Type { op_equal(Self, Self) -> Bool size_of(Self, Int) -> Int to_json(Self) -> Json - to_string(Self) -> String } +impl Show for Type // Type aliases // Traits -// Extension Methods -impl Show for Fundef - -impl Show for Kind - -impl Show for Name - -impl Show for Op - -impl Show for Syntax - -impl Show for Type - diff --git a/src/parser/parser.mbti b/src/parser/parser.mbti index d57194e..613c2bd 100644 --- a/src/parser/parser.mbti +++ b/src/parser/parser.mbti @@ -8,5 +8,3 @@ package moonbitlang/minimbt/parser // Traits -// Extension Methods - diff --git a/src/riscv/riscv.mbti b/src/riscv/riscv.mbti index 1f98808..23a884e 100644 --- a/src/riscv/riscv.mbti +++ b/src/riscv/riscv.mbti @@ -1,8 +1,6 @@ package moonbitlang/minimbt/riscv // Values -fn asm_stringify(AssemblyFunction) -> String - fn emit(Unit) -> Array[AssemblyFunction] let freg_allocatable_list : Array[FReg] @@ -21,6 +19,8 @@ let freg_swap : FReg let freg_temp_list : Array[FReg] +fn print_functions(Array[AssemblyFunction]) -> String + let reg_allocatable_list : Array[Reg] let reg_arg_list : Array[Reg] @@ -42,16 +42,16 @@ let reg_swap : Reg let reg_temp_list : Array[Reg] // Types and methods -pub struct AssemblyFunction { - pub name : String - pub export : Bool - pub body : Array[RvAsm] +pub(all) struct AssemblyFunction { + name : String + export : Bool + body : Array[RvAsm] } impl AssemblyFunction { - output(Self, Logger) -> Unit + output(Self, &Logger) -> Unit } -pub enum FReg { +pub(all) enum FReg { Ft0 Ft1 Ft2 @@ -86,25 +86,21 @@ pub enum FReg { Ft11 } impl FReg { - compare(Self, Self) -> Int - hash_combine(Self, Hasher) -> Unit - op_equal(Self, Self) -> Bool to_string(Self) -> String } +impl Compare for FReg +impl Eq for FReg +impl Hash for FReg type Label -impl Label { - hash_combine(Self, Hasher) -> Unit - op_equal(Self, Self) -> Bool - to_string(Self) -> String -} +impl Eq for Label +impl Hash for Label +impl Show for Label type MemAccess -impl MemAccess { - to_string[TBase : Show, TOff : Show](Self[TBase, TOff]) -> String -} +impl[TBase : Show, TOff : Show] Show for MemAccess[TBase, TOff] -pub enum Reg { +pub(all) enum Reg { Zero Ra Sp @@ -139,13 +135,13 @@ pub enum Reg { T6 } impl Reg { - compare(Self, Self) -> Int - hash_combine(Self, Hasher) -> Unit - op_equal(Self, Self) -> Bool to_string(Self) -> String } +impl Compare for Reg +impl Eq for Reg +impl Hash for Reg -pub enum RvAsm { +pub(all) enum RvAsm { Add(Reg, Reg, Reg) Sub(Reg, Reg, Reg) Xor(Reg, Reg, Reg) @@ -202,8 +198,8 @@ pub enum RvAsm { FdivD(FReg, FReg, FReg) Fld(FReg, MemAccess[Reg, Int]) Fsd(FReg, MemAccess[Reg, Int]) - FbeqD(FReg, FReg, Label) - FbleD(FReg, FReg, Label) + FeqD(Reg, FReg, FReg) + FleD(Reg, FReg, FReg) FmvDX(FReg, Reg) Nop La(Reg, Label) @@ -226,8 +222,3 @@ pub enum RvAsm { // Traits -// Extension Methods -impl Show for Label - -impl Show for MemAccess - diff --git a/src/typing/typing.mbti b/src/typing/typing.mbti index 1d86cdc..c7bcd65 100644 --- a/src/typing/typing.mbti +++ b/src/typing/typing.mbti @@ -8,5 +8,3 @@ package moonbitlang/minimbt/typing // Traits -// Extension Methods - diff --git a/src/util/util.mbti b/src/util/util.mbti index aea70ab..36d7168 100644 --- a/src/util/util.mbti +++ b/src/util/util.mbti @@ -11,15 +11,13 @@ fn i64_to_hex(Int64) -> String type IndentLogger impl IndentLogger { indent(Self) -> Unit - new(Logger) -> Self + new(&Logger) -> Self outdent(Self) -> Unit write_string(Self, String) -> Unit } +impl Logger for IndentLogger // Type aliases // Traits -// Extension Methods -impl Logger for IndentLogger -