Skip to content

Commit c7d0c40

Browse files
committed
Update to moonbit v0.1.20250423+2af95d42c
1 parent 1180dee commit c7d0c40

File tree

7 files changed

+61
-47
lines changed

7 files changed

+61
-47
lines changed

moon.mod.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "nathsou/smb",
33
"version": "0.1.0",
44
"deps": {
5-
"moonbitlang/x": "0.4.11"
5+
"moonbitlang/x": "0.4.26"
66
},
77
"readme": "README.md",
88
"repository": "",

src/lower/lower.mbt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub(all) struct AsmLowering {
1414
flat_subroutines: Array[AsmFlatSubroutine]
1515
} derive (Show)
1616

17-
pub(readonly) type! LoweringError String derive(Show)
17+
pub type! LoweringError String derive(Show)
1818

1919
pub fn AsmLowering::new(items: Array[@parse.SourceItem]) -> AsmLowering {
2020
{
@@ -312,7 +312,7 @@ fn collect_jump_targets(self: AsmLowering) -> Unit {
312312
}
313313
Label(name) => {
314314
// manually annotated subroutines
315-
if name.ends_with("__sub") {
315+
if name.has_suffix("__sub") {
316316
self.targets.subroutines.add(name)
317317
}
318318
}
@@ -379,7 +379,7 @@ fn collect_basic_block_leaders(self: AsmLowering) -> Array[Int] {
379379
leader_indices
380380
}
381381

382-
struct ItemInfo {
382+
priv struct ItemInfo {
383383
item: AsmItem
384384
index: Int
385385
}
@@ -472,7 +472,7 @@ fn collect_basic_blocks(self: AsmLowering) -> Unit!LoweringError {
472472
connect(bb, target_bb, false)
473473
}
474474
None => {
475-
if not(label.starts_with("__")) { // allow special labels
475+
if not(label.has_prefix("__")) { // allow special labels
476476
raise LoweringError("Unknown jump target: \{label}")
477477
}
478478
}
@@ -485,7 +485,7 @@ fn collect_basic_blocks(self: AsmLowering) -> Unit!LoweringError {
485485
connect(bb, target_bb, false)
486486
}
487487
None => {
488-
if not(label.starts_with("__")) { // allow special labels
488+
if not(label.has_prefix("__")) { // allow special labels
489489
raise LoweringError("Unknown jump target: \{label}")
490490
}
491491
}
@@ -591,7 +591,7 @@ fn rewrite_bb(self: AsmLowering, bb: BasicBlock, visited: Set[Int]) -> Unit!Lowe
591591
}
592592

593593
pub fn is_subroutine_label(self: AsmLowering, label: String) -> Bool {
594-
self.targets.subroutines.contains(label) || label.ends_with("__sub")
594+
self.targets.subroutines.contains(label) || label.has_suffix("__sub")
595595
}
596596

597597
fn collect_subroutines(self: AsmLowering) -> Unit!LoweringError {
@@ -677,7 +677,7 @@ fn _cfg_dotfile(self: AsmLowering) -> String {
677677
builder.to_string()
678678
}
679679

680-
struct ControlFlowStackEntry {
680+
priv struct ControlFlowStackEntry {
681681
exit_label: String
682682
cond: @parse.OpCode
683683
body: Array[AsmItem]

src/main/main.mbt

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ type! PipelineError {
77
} derive(Show)
88

99
fn read_rom() -> String!PipelineError {
10-
match @fs.read_file_to_string?(path="src/smb.asm") {
10+
match @fs.read_file_to_string?("src/smb.asm") {
1111
Ok(asm) => asm
1212
Err(err) => raise IOError(Show::to_string(err))
1313
}
@@ -53,23 +53,32 @@ fn transpile() -> @transpile.Transpiler!PipelineError {
5353
let lib_output_dir: String = "out/lib"
5454
let output_dir: String = "out"
5555

56-
fn main {
57-
if not(@fs.path_exists(path=output_dir)) {
58-
@fs.create_dir(path=output_dir)
56+
fn run() -> Unit!Error {
57+
if not(@fs.path_exists(output_dir)) {
58+
@fs.create_dir!(output_dir)
5959
}
6060

61-
if not(@fs.path_exists(path=lib_output_dir)) {
62-
@fs.create_dir(path=lib_output_dir)
61+
if not(@fs.path_exists(lib_output_dir)) {
62+
@fs.create_dir!(lib_output_dir)
6363
}
6464

6565
match transpile?() {
6666
Ok(tr) => {
6767
for file in tr.files {
68-
file.write(dir=if file.is_lib { lib_output_dir } else { output_dir })
68+
file.write!(dir=if file.is_lib { lib_output_dir } else { output_dir })
6969
}
7070
}
7171
Err(err) => {
7272
println(err)
7373
}
7474
}
7575
}
76+
77+
fn main {
78+
match run?() {
79+
Ok(_) => ()
80+
Err(err) => {
81+
println("Error: \{Show::to_string(err)}")
82+
}
83+
}
84+
}

src/parse/lex.mbt

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pub(all) struct Lexer {
44
mut index: Int
55
}
66

7-
pub(readonly) type! LexerError {
7+
pub type! LexerError {
88
UnexepectedChar(char~: Char, index~: Int)
99
InvalidConstant(lexeme~: String, base~: ConstantBase)
1010
} derive(Show)
@@ -117,9 +117,14 @@ fn parse_constant(self: Lexer, base~: ConstantBase) -> LexerResult {
117117

118118
let lexeme = lexeme.to_string()
119119

120-
let value = match @strconv.parse_int?(lexeme, base=base.to_int()) {
121-
Ok(value) => value
122-
Err(_) => return Grr(InvalidConstant(lexeme~, base~))
120+
let value = match (lexeme, base) {
121+
("0b", Hexadecimal) => 11 // https://github.com/moonbitlang/core/issues/2006
122+
_ => {
123+
match @strconv.parse_int?(lexeme, base=base.to_int()) {
124+
Ok(value) => value
125+
Err(_) => return Grr(InvalidConstant(lexeme~, base~))
126+
}
127+
}
123128
}
124129

125130
let size = match base {
@@ -285,7 +290,7 @@ fn advance(self: Lexer) -> Unit {
285290
self.index += 1
286291
}
287292

288-
enum LexerResult {
293+
priv enum LexerResult {
289294
Yay(Token)
290295
Grr(LexerError)
291296
EOF

src/parse/parse.mbt

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pub(all) struct Parser {
44
mut index: Int
55
}
66

7-
pub(readonly) type! ParserError {
7+
pub type! ParserError {
88
UnexpectedToken(expected~: Token, received~: Token)
99
ExpectedIdentifier(Token)
1010
ExpectedExpr(Token)
@@ -15,15 +15,15 @@ pub fn Parser::new(tokens: Array[Token]) -> Parser {
1515
{ tokens, index: 0, defines: {} }
1616
}
1717

18-
fn peek(self: Parser) -> Token {
18+
fn Parser::peek(self: Parser) -> Token {
1919
self.tokens[self.index]
2020
}
2121

22-
fn lookahead(self: Parser, n: Int) -> Token {
22+
fn Parser::lookahead(self: Parser, n: Int) -> Token {
2323
self.tokens[self.index + n]
2424
}
2525

26-
fn advance(self: Parser) -> Unit {
26+
fn Parser::advance(self: Parser) -> Unit {
2727
self.index += 1
2828
}
2929

@@ -36,15 +36,15 @@ fn expect(self: Parser, expected: Token) -> Unit!ParserError {
3636
}
3737
}
3838

39-
fn matches(self: Parser, token: Token) -> Bool {
39+
fn Parser::matches(self: Parser, token: Token) -> Bool {
4040
self.peek() == token
4141
}
4242

4343
fn matches_predicate(self: Parser, predicate: (Token) -> Bool) -> Bool {
4444
predicate(self.peek())
4545
}
4646

47-
fn consumes(self: Parser, token: Token) -> Bool {
47+
fn Parser::consumes(self: Parser, token: Token) -> Bool {
4848
if self.matches(token) {
4949
self.advance()
5050
true
@@ -53,12 +53,12 @@ fn consumes(self: Parser, token: Token) -> Bool {
5353
}
5454
}
5555

56-
fn consumeIfPresent(self: Parser, token: Token) -> Unit {
56+
fn Parser::consumeIfPresent(self: Parser, token: Token) -> Unit {
5757
let _ = self.consumes(token)
5858

5959
}
6060

61-
fn parse_identifier(self: Parser) -> String!ParserError {
61+
fn Parser::parse_identifier(self: Parser) -> String!ParserError {
6262
let token = self.peek()
6363
match token {
6464
Identifier(name) => {
@@ -69,7 +69,7 @@ fn parse_identifier(self: Parser) -> String!ParserError {
6969
}
7070
}
7171

72-
fn parse_primary_expr(self: Parser) -> SourceExpr!ParserError {
72+
fn Parser::parse_primary_expr(self: Parser) -> SourceExpr!ParserError {
7373
let token = self.peek()
7474
match token {
7575
Constant(base~, value~, size~) => {
@@ -100,7 +100,7 @@ fn parse_primary_expr(self: Parser) -> SourceExpr!ParserError {
100100
}
101101
}
102102

103-
fn parse_additive_expr(self: Parser) -> SourceExpr!ParserError {
103+
fn Parser::parse_additive_expr(self: Parser) -> SourceExpr!ParserError {
104104
let mut lhs = self.parse_primary_expr!()
105105
while true {
106106
if self.consumes(Plus) {
@@ -116,11 +116,11 @@ fn parse_additive_expr(self: Parser) -> SourceExpr!ParserError {
116116
lhs
117117
}
118118

119-
fn parse_expr(self: Parser) -> SourceExpr!ParserError {
119+
fn Parser::parse_expr(self: Parser) -> SourceExpr!ParserError {
120120
self.parse_additive_expr!()
121121
}
122122

123-
fn parse_directive_args(self: Parser) -> Array[SourceExpr]!ParserError {
123+
fn Parser::parse_directive_args(self: Parser) -> Array[SourceExpr]!ParserError {
124124
let args: Array[SourceExpr] = []
125125
while true {
126126
let expr = self.parse_expr!()
@@ -132,7 +132,7 @@ fn parse_directive_args(self: Parser) -> Array[SourceExpr]!ParserError {
132132
args
133133
}
134134

135-
fn parse_addressing_mode(
135+
fn Parser::parse_addressing_mode(
136136
self: Parser,
137137
opcode~: OpCode
138138
) -> SourceAddressingMode!ParserError {
@@ -200,7 +200,7 @@ fn parse_addressing_mode(
200200
}
201201
}
202202

203-
fn parse_inst(self: Parser) -> SourceItem!ParserError {
203+
fn Parser::parse_inst(self: Parser) -> SourceItem!ParserError {
204204
let token = self.peek()
205205

206206
match token {
@@ -232,7 +232,7 @@ fn parse_inst(self: Parser) -> SourceItem!ParserError {
232232
}
233233
}
234234

235-
pub fn parse(self: Parser) -> Array[SourceItem]!ParserError {
235+
pub fn Parser::parse(self: Parser) -> Array[SourceItem]!ParserError {
236236
let insts: Array[SourceItem] = []
237237

238238
while self.index < self.tokens.length() {

src/parse/token.mbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ enum Token {
3131
RightChevron
3232
} derive (Show, Eq)
3333

34-
fn to_int(self: ConstantBase) -> Int {
34+
fn ConstantBase::to_int(self: ConstantBase) -> Int {
3535
match self {
3636
Binary => 2
3737
Decimal => 10

src/transpile/transpile.mbt

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ fn emit_data_c(self: Emitter, low: @lower.AsmLowering) -> Unit {
147147
for line in table.lines {
148148
match line {
149149
Data(data~, comment~) => {
150-
let data = String::concat(data.map(show_expr), separator=", ")
150+
let data = @string.concat(data.map(show_expr), separator=", ")
151151

152152
match comment.val {
153153
"" => self.emit(data + ",")
@@ -185,8 +185,8 @@ fn emit_code_h(self: Emitter, low: @lower.AsmLowering) -> Unit {
185185
}
186186

187187
fn rename_label(label: String) -> String {
188-
if label.ends_with("__sub") {
189-
label.split("__sub").iter().peek().unwrap()
188+
if label.has_suffix("__sub") {
189+
label.split("__sub").iter().peek().unwrap().to_string()
190190
} else {
191191
label
192192
}
@@ -221,15 +221,15 @@ fn show_cond_opcode(op: @parse.OpCode) -> String {
221221
}
222222
}
223223

224-
enum Reg { A; X; Y }
225-
struct Range { min: Int; max: Int }
224+
priv enum Reg { A; X; Y }
225+
priv struct Range { min: Int; max: Int }
226226

227-
fn op_add(self: Range, other: Range) -> Range {
227+
impl Add for Range with op_add(self: Range, other: Range) -> Range {
228228
{ min: self.min + other.min, max: self.max + other.max }
229229
}
230230

231-
fn op_sub(self: Range, other: Range) -> Range {
232-
{ min: self.min - other.max, max: self.max - other.min }
231+
impl Sub for Range with op_sub(self: Range, other: Range) -> Range {
232+
{ min: self.min - other.min, max: self.max - other.max }
233233
}
234234

235235
fn compute_expr_range(expr: @lower.TypedExpr, low: @lower.AsmLowering) -> Range? {
@@ -498,12 +498,12 @@ pub(all) struct File {
498498
}
499499

500500
fn File::read(name~: String, path~: String, is_lib~: Bool = true) -> File!Error {
501-
let contents = @fs.read_file_to_string!(path~)
501+
let contents = @fs.read_file_to_string!(path)
502502
{ name, contents, is_lib }
503503
}
504504

505-
pub fn write(self: File, dir~: String) -> Unit {
506-
@fs.write_string_to_file(path="\{dir}/\{self.name}", content=self.contents)
505+
pub fn File::write(self: File, dir~: String) -> Unit!Error {
506+
@fs.write_string_to_file!("\{dir}/\{self.name}", self.contents)
507507
}
508508

509509
pub(all) struct Transpiler {

0 commit comments

Comments
 (0)