Skip to content

Commit

Permalink
Merge pull request #22 from moonbitlang/tweak
Browse files Browse the repository at this point in the history
tweak: remove unstable_io & unused import
  • Loading branch information
Young-Flash authored Dec 3, 2024
2 parents a1ee371 + 2ac82ac commit 8e020dd
Show file tree
Hide file tree
Showing 35 changed files with 213 additions and 98 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/moonbit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:

- name: install
run: |
curl -fsSL https://cli.moonbitlang.com/install/unix.sh | bash -s minimoonbit
curl -fsSL https://cli.moonbitlang.com/install/unix.sh | bash
echo "$HOME/.moon/bin" >> $GITHUB_PATH
- name: moon version
Expand Down
2 changes: 1 addition & 1 deletion moon.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "moonbitlang/minimbt",
"version": "0.1.0",
"deps": {
"lijunchen/unstable_io": "0.1.3",
"moonbitlang/x": "0.4.13",
"Yoorkin/ArgParser": "0.1.6"
},
"readme": "README.md",
Expand Down
5 changes: 2 additions & 3 deletions moonbit.public.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ RUN wget https://nodejs.org/dist/v22.10.0/node-v22.10.0-linux-x64.tar.xz -O /tmp
RUN mkdir -p /root/.zig /root/.moon/bin /root/bin

# Install MoonBit toolchain
RUN curl -fsSL https://cli.moonbitlang.cn/install/unix.sh | bash -s minimoonbit
RUN curl -fsSL https://cli.moonbitlang.cn/install/unix.sh | bash

# Install zig toolchain
RUN curl -fsSL https://ziglang.org/download/0.13.0/zig-linux-x86_64-0.13.0.tar.xz | tar -xJf - -C /root/.zig --strip-components 1
Expand Down Expand Up @@ -62,8 +62,7 @@ WORKDIR /app
WORKDIR /tmp
RUN moon new foo
WORKDIR /tmp/foo
# RUN moon add moonbitlang/x
RUN moon add lijunchen/unstable_io
RUN moon add moonbitlang/x
RUN moon add Yoorkin/ArgParser
RUN moon add Yoorkin/trie
RUN moon check
Expand Down
11 changes: 7 additions & 4 deletions src/bin/externals.mbt
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
///|
fn add_interpreter_fns(interpreter : @knf_eval.KnfInterpreter) -> Unit {
interpreter.add_extern_fn(
"minimbt_print_int",
fn(args) {
match args[0] {
Int(i) => @io.print(i)
Int(i) => println(i)
_ => @util.die("print_int expects Int")
}
Unit
Expand All @@ -12,7 +13,7 @@ fn add_interpreter_fns(interpreter : @knf_eval.KnfInterpreter) -> Unit {
interpreter.add_extern_fn(
"minimbt_print_endline",
fn(_args) {
@io.print("\n")
println("")
Unit
},
)
Expand Down Expand Up @@ -90,14 +91,15 @@ fn add_interpreter_fns(interpreter : @knf_eval.KnfInterpreter) -> Unit {
)
}

///|
fn add_closure_interpreter_fns(
interpreter : @closure_eval.ClosureInterpreter
) -> Unit {
interpreter.add_extern_fn(
"minimbt_print_int",
fn(args) {
match args[0] {
Int(i) => @io.print(i)
Int(i) => println(i)
_ => @util.die("print_int expects Int")
}
Unit
Expand All @@ -106,7 +108,7 @@ fn add_closure_interpreter_fns(
interpreter.add_extern_fn(
"minimbt_print_endline",
fn(_args) {
@io.print("\n")
println("")
Unit
},
)
Expand Down Expand Up @@ -184,6 +186,7 @@ fn add_closure_interpreter_fns(
)
}

///|
fn externals() -> @immut/hashmap.T[String, @types.Type] {
@immut/hashmap.T::new()
.add("read_int", @types.Fun([], Int))
Expand Down
21 changes: 13 additions & 8 deletions src/bin/main.mbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// The stage to be run next.
///| The stage to be run next.
enum Stages {
Parse
Typecheck
Expand All @@ -9,6 +9,7 @@ enum Stages {
Finished
} derive(Show, Eq, Compare)

///|
fn Stages::from_string(s : String) -> Stages? {
match s {
"parse" => Some(Stages::Parse)
Expand All @@ -22,6 +23,7 @@ fn Stages::from_string(s : String) -> Stages? {
}
}

///|
fn Stages::next(self : Stages) -> Stages {
match self {
Stages::Parse => Stages::Typecheck
Expand All @@ -34,6 +36,7 @@ fn Stages::next(self : Stages) -> Stages {
}
}

///|
struct CompileStatus {
mut curr_stage : Stages
end_stage : Stages
Expand All @@ -47,6 +50,7 @@ struct CompileStatus {
mut asm : Array[@riscv.AssemblyFunction]?
}

///|
fn CompileStatus::initialize(
start_stage : Stages,
end_stage : Stages,
Expand Down Expand Up @@ -81,14 +85,13 @@ fn CompileStatus::initialize(
v
}

/// Run the next stage of compilation. Returns true if the compilation is finished.
///| Run the next stage of compilation. Returns true if the compilation is finished.
fn CompileStatus::step(self : CompileStatus) -> Bool {
if self.curr_stage >= self.end_stage {
return true
}
match self.curr_stage {
Parse => {
let source_code = self.source_code.unwrap()
let parsed = @util.die("TODO: parse")
self.ast = Some(parsed)
}
Expand Down Expand Up @@ -122,6 +125,7 @@ fn CompileStatus::step(self : CompileStatus) -> Bool {
self.curr_stage >= self.end_stage
}

///|
fn CompileStatus::output(self : CompileStatus, json : Bool) -> String {
if json {
match self.curr_stage {
Expand All @@ -146,8 +150,9 @@ fn CompileStatus::output(self : CompileStatus, json : Bool) -> String {
}
}

///|
fn main {
let argv = @env.get_args()
let argv = @sys.get_cli_args()
let mut file = None
let knf_opt_iters = Ref::new(10)
let knf_opt_inline_threshold = Ref::new(10)
Expand Down Expand Up @@ -225,7 +230,7 @@ fn main {
let i = @strconv.parse_int?(s)
match i {
Ok(i) => knf_opt_iters.val = i
Err(e) => @util.die("Invalid number")
Err(_e) => @util.die("Invalid number")
}
},
),
Expand All @@ -239,7 +244,7 @@ fn main {
let i = @strconv.parse_int?(s)
match i {
Ok(i) => knf_opt_inline_threshold.val = i
Err(e) => @util.die("Invalid number")
Err(_e) => @util.die("Invalid number")
}
},
),
Expand Down Expand Up @@ -278,7 +283,7 @@ fn main {
} else {
file.unwrap()
}
let contents = @fs.read_to_string(file)
let contents = @fs.read_file_to_string?(path=file).unwrap()

// Compilation
let status = match
Expand Down Expand Up @@ -329,7 +334,7 @@ fn main {
if out_file.val == "-" {
println(out_string)
} else {
@fs.write_to_string(out_file.val, out_string)
@fs.write_string_to_file(path=out_file.val, content=out_string)
}
}
}
8 changes: 2 additions & 6 deletions src/bin/moon.pkg.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
{
"is-main": true,
"import": [
"lijunchen/unstable_io/env",
"moonbitlang/x/sys",
"Yoorkin/ArgParser",
"lijunchen/unstable_io/fs",
"moonbitlang/minimbt/parser",
"moonbitlang/minimbt/lex",
{
"path": "moonbitlang/minimbt",
"alias": "types"
},
"moonbitlang/minimbt/knf",
"moonbitlang/minimbt/typing",
"moonbitlang/minimbt/knf_eval",
"lijunchen/unstable_io/io",
"moonbitlang/x/fs",
"moonbitlang/minimbt/closure",
"moonbitlang/minimbt/riscv",
"moonbitlang/minimbt/util",
Expand Down
17 changes: 12 additions & 5 deletions src/closure/closure_ir.mbt
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
pub type Label String derive(Show)
///|
pub(all) type Label String derive(Show)

///|
pub typealias Name = @types.Name

///|
pub typealias Type = @types.Type

pub enum Expr {
///|
pub(all) enum Expr {
Unit
Int(Int)
Double(Double)
Expand Down Expand Up @@ -32,12 +36,14 @@ pub enum Expr {
ExternalArray(Label)
} derive(Show)

pub struct Closure {
///|
pub(all) struct Closure {
name : Label
actual_free_vars : Array[Name]
} derive(Show)

pub struct FuncDef {
///|
pub(all) struct FuncDef {
name : Label
old_name : Name
/// true if the function is a closure function, and the closure will be available at `s11` on
Expand All @@ -49,7 +55,8 @@ pub struct FuncDef {
body : Expr
} derive(Show)

pub struct Program {
///|
pub(all) struct Program {
fundefs : Array[FuncDef]
body : Expr
} derive(Show)
10 changes: 10 additions & 0 deletions src/closure/json.mbt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
///|
pub fn Expr::to_json(self : Expr) -> Json {
match self {
Expr::Unit => ["Unit"]
Expand Down Expand Up @@ -40,6 +41,7 @@ pub fn Expr::to_json(self : Expr) -> Json {
}
}

///|
pub fn Expr::from_json(json : Json) -> Expr! {
match json {
["Unit"] => Expr::Unit
Expand Down Expand Up @@ -126,6 +128,7 @@ pub fn Expr::from_json(json : Json) -> Expr! {
}
}

///|
pub fn LowType::to_json(self : LowType) -> Json {
match self {
LowType::Unit => ["Unit"]
Expand All @@ -140,6 +143,7 @@ pub fn LowType::to_json(self : LowType) -> Json {
}
}

///|
pub fn LowType::from_json(json : Json) -> LowType! {
match json {
["Unit"] => LowType::Unit
Expand Down Expand Up @@ -173,13 +177,15 @@ pub fn LowType::from_json(json : Json) -> LowType! {
}
}

///|
pub fn Closure::to_json(self : Closure) -> Json {
{
"name": self.name._.to_json(),
"actual_free_vars": self.actual_free_vars.to_json(),
}
}

///|
pub fn Closure::from_json(json : Json) -> Closure! {
match json {
{ "name": String(name), "actual_free_vars": Array(actual_free_vars) } => {
Expand All @@ -193,6 +199,7 @@ pub fn Closure::from_json(json : Json) -> Closure! {
}
}

///|
pub fn FuncDef::to_json(self : FuncDef) -> Json {
{
"name": self.name._.to_json(),
Expand All @@ -219,6 +226,7 @@ pub fn FuncDef::to_json(self : FuncDef) -> Json {
}
}

///|
pub fn FuncDef::from_json(json : Json) -> FuncDef! {
match json {
{
Expand Down Expand Up @@ -261,13 +269,15 @@ pub fn FuncDef::from_json(json : Json) -> FuncDef! {
}
}

///|
pub fn Program::to_json(self : Program) -> Json {
{
"fundefs": Array(self.fundefs.map(FuncDef::to_json)),
"body": self.body.to_json(),
}
}

///|
pub fn Program::from_json(json : Json) -> Program! {
match json {
{ "fundefs": Array(fundefs), "body": body } => {
Expand Down
6 changes: 4 additions & 2 deletions src/closure/knf_to_closure.mbt
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
///|
pub typealias LocalEnv = @immut/hashmap.T[Name, LowType]

///|
pub fn knf_program_to_closure(
prog : @knf.Knf,
extern_env : Map[String, Type]
_prog : @knf.Knf,
_extern_env : Map[String, Type]
) -> Program {
abort("todo")
}
7 changes: 5 additions & 2 deletions src/closure/lowtype.mbt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// Low-level types. Main difference between this and Type is that it distinguishes between direct function pointers and closure function pointers.
pub enum LowType {
///| Low-level types. Main difference between this and Type is that it distinguishes between direct function pointers and closure function pointers.
pub(all) enum LowType {
Unit
Bool
Int
Expand All @@ -11,20 +11,23 @@ pub enum LowType {
Ptr
} derive(Show)

///|
pub fn LowType::is_ptr_like(self : LowType) -> Bool {
match self {
ClosureFn(_) | DirectFn(_) | Tuple(_) | Array(_) | Ptr => true
_ => false
}
}

///|
pub fn LowType::is_float_like(self : LowType) -> Bool {
match self {
Double => true
_ => false
}
}

///|
pub fn LowType::size_of(self : LowType, size_of_ptr : Int) -> Int {
match self {
Unit => 0
Expand Down
3 changes: 1 addition & 2 deletions src/closure/moon.pkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"path": "moonbitlang/minimbt",
"alias": "types"
},
"moonbitlang/minimbt/knf",
"moonbitlang/minimbt/util"
"moonbitlang/minimbt/knf"
]
}
Loading

0 comments on commit 8e020dd

Please sign in to comment.