-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
797 additions
and
391 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
(module | ||
;; Import the external function 'env.print' (equivalent to print in the original code) | ||
(import "env" "print" (func $print (param i32))) | ||
|
||
;; Define the factorial function | ||
(func $factorial (param $n i32) (result i32) | ||
;; Local variables | ||
(local $result i32) | ||
|
||
;; If statement: if (n == 0) | ||
(if (result i32) | ||
(i32.eq (local.get $n) (i32.const 0)) | ||
;; Then block: rt 1 | ||
(then | ||
(i32.const 1) | ||
) | ||
;; Else block: rt n * factorial(n - 1) | ||
(else | ||
;; Recursive call to factorial(n - 1) | ||
(call $factorial (i32.sub (local.get $n) (i32.const 1))) | ||
(local.set $result) | ||
(i32.mul (local.get $n) (local.get $result)) | ||
) | ||
) | ||
) | ||
|
||
;; Define the main function | ||
(func $main (param $result i32) | ||
;; Calculate factorial(5) | ||
(i32.const 5) | ||
(call $factorial) | ||
|
||
;; Call print with the result | ||
(call $print (local.get $result)) | ||
) | ||
|
||
;; Export the main function so that it can be called externally | ||
(export "main" (func $main)) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
import { Range } from "../Range"; | ||
import { Identifier } from "./Identifier"; | ||
import { Statement } from "./Statement"; | ||
import { StructMember } from "./StructMember"; | ||
import { StructFieldDeclaration } from "./StructFieldDeclaration"; | ||
|
||
export class StructDeclaration extends Statement { | ||
public nameOf = "StructDeclaration"; | ||
public name: Identifier; | ||
public members: StructMember[]; | ||
constructor(name: Identifier, members: StructMember[], range: Range) { | ||
public fields: StructFieldDeclaration[]; | ||
constructor(name: Identifier, fields: StructFieldDeclaration[], range: Range) { | ||
super(); | ||
this.name = name; | ||
this.members = members; | ||
this.fields = fields; | ||
this.range = range; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
#[export] | ||
fn add(a: i32, b: i32) -> i32 { | ||
rt a + b | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#[extern]: env.print | ||
fn print(num: i32) -> void | ||
|
||
fn factorial(n: i32) -> i32 { | ||
if (n == 0) { | ||
rt 1 | ||
} else { | ||
rt n * factorial(n - 1) | ||
} | ||
} | ||
|
||
#[export] | ||
fn main() -> void { | ||
i32 result = factorial(5) | ||
print(result) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.