fn declaration adjustments

This commit is contained in:
Nilton Constantino 2026-01-29 15:45:48 +00:00
parent b814a6c48d
commit 9de23475f6
No known key found for this signature in database
14 changed files with 90 additions and 13 deletions

View File

@ -197,7 +197,7 @@ impl Parser {
let start_span = self.consume(TokenKind::Fn)?.span;
let name = self.expect_identifier()?;
let params = self.parse_param_list()?;
let ret = if self.peek().kind == TokenKind::Arrow {
let ret = if self.peek().kind == TokenKind::Colon {
self.advance();
Box::new(self.parse_type_ref()?)
} else {
@ -275,7 +275,7 @@ impl Parser {
let start_span = self.consume(TokenKind::Fn)?.span;
let name = self.expect_identifier()?;
let params = self.parse_param_list()?;
let _ret = if self.peek().kind == TokenKind::Arrow {
let _ret = if self.peek().kind == TokenKind::Colon {
self.advance();
Some(Box::new(self.parse_type_ref()?))
} else {

View File

@ -31,7 +31,7 @@ import math from "./math.pbs";
#[test]
fn test_parse_fn_decl() {
let source = r#"
fn add(a: int, b: int) -> int {
fn add(a: int, b: int): int {
return a + b;
}
"#;
@ -71,7 +71,7 @@ fn test_parse_service_decl() {
let source = r#"
pub service Audio {
fn play(sound: Sound);
fn stop() -> bool;
fn stop(): bool;
}
"#;
let mut parser = Parser::new(source, 0);

View File

@ -21,7 +21,7 @@ fn test_compile_hip_program() {
// Create main.pbs with HIP effects
let code = "
fn main() {
fn frame(): void {
let x = alloc int;
mutate x as v {
let y = v + 1;

View File

@ -20,7 +20,7 @@ fn test_golden_bytecode_snapshot() {
let code = r#"
declare contract Gfx host {}
fn helper(val: int) -> int {
fn helper(val: int): int {
return val * 2;
}

View File

@ -7,7 +7,7 @@ use prometeu_compiler::ir_core;
#[test]
fn test_basic_lowering() {
let code = "
fn add(a: int, b: int) -> int {
fn add(a: int, b: int): int {
return a + b;
}
fn main() {
@ -43,7 +43,7 @@ fn test_basic_lowering() {
#[test]
fn test_control_flow_lowering() {
let code = "
fn max(a: int, b: int) -> int {
fn max(a: int, b: int): int {
if (a > b) {
return a;
} else {

View File

@ -36,7 +36,7 @@ fn test_type_mismatch_let() {
#[test]
fn test_type_mismatch_return() {
let code = "fn main() -> int { return \"hello\"; }";
let code = "fn main(): int { return \"hello\"; }";
let res = check_code(code);
assert!(res.is_err());
assert!(res.unwrap_err().contains("E_TYPE_MISMATCH"));
@ -57,7 +57,7 @@ fn test_type_mismatch_call() {
#[test]
fn test_missing_return_path() {
let code = "fn foo() -> int { if (true) { return 1; } }";
let code = "fn foo(): int { if (true) { return 1; } }";
let res = check_code(code);
assert!(res.is_err());
assert!(res.unwrap_err().contains("E_TYPE_RETURN_PATH"));
@ -65,7 +65,7 @@ fn test_missing_return_path() {
#[test]
fn test_implicit_none_optional() {
let code = "fn foo() -> optional<int> { if (true) { return some(1); } }";
let code = "fn foo(): optional<int> { if (true) { return some(1); } }";
let res = check_code(code);
if let Err(e) = &res { println!("Error: {}", e); }
assert!(res.is_ok()); // Implicit none allowed for optional
@ -82,7 +82,7 @@ fn test_valid_optional_assignment() {
#[test]
fn test_valid_result_usage() {
let code = "
fn foo() -> result<int, string> {
fn foo(): result<int, string> {
if (true) {
return ok(10);
} else {
@ -160,7 +160,7 @@ fn test_struct_type_usage() {
fn test_service_type_usage() {
let code = "
pub service MyService {
fn hello(name: string) -> void
fn hello(name: string): void
}
fn foo(s: MyService) {}
";

View File

@ -0,0 +1,41 @@
{
"magic": "PMTU",
"cartridge_version": 1,
"app_id": 1,
"title": "Test 1",
"app_version": "0.1.0",
"app_mode": "Game",
"entrypoint": "0",
"asset_table": [
{
"asset_id": 0,
"asset_name": "bgm_music",
"bank_type": "SOUNDS",
"offset": 0,
"size": 88200,
"decoded_size": 88200,
"codec": "RAW",
"metadata": {
"sample_rate": 44100
}
},
{
"asset_id": 1,
"asset_name": "mouse_cursor",
"bank_type": "TILES",
"offset": 88200,
"size": 2304,
"decoded_size": 2304,
"codec": "RAW",
"metadata": {
"tile_size": 16,
"width": 16,
"height": 16
}
}
],
"preload": [
{ "asset_name": "bgm_music", "slot": 0 },
{ "asset_name": "mouse_cursor", "slot": 1 }
]
}

View File

@ -0,0 +1,12 @@
00000000 PushConst U32(1)
00000006 SetLocal U32(0)
0000000C GetLocal U32(0)
00000012 PushConst U32(1)
00000018 Eq
0000001A JmpIfFalse U32(56)
00000020 Jmp U32(38)
00000026 PushConst U32(2)
0000002C Syscall U32(4097)
00000032 Jmp U32(62)
00000038 Jmp U32(62)
0000003E Ret

Binary file not shown.

View File

@ -0,0 +1,7 @@
{
"script_fe": "pbs",
"entry": "src/main.pbs",
"out": "build/program.pbc",
"emit_disasm": true,
"emit_symbols": true
}

1
test-cartridges/test01/sdk Symbolic link
View File

@ -0,0 +1 @@
../../target/debug

View File

@ -0,0 +1,6 @@
declare contract Touch host {
fn f();
}
fn frame(): void {
}

View File

@ -0,0 +1,3 @@
pub declare contract Gfx host {}
pub declare contract Input host {}
pub declare contract Touch host {}

View File

@ -0,0 +1,7 @@
import Gfx from "./sdk.pbs"
fn frame(): void {
Gfx.clear(0);
Gfx.fillRect(110, 110, 20, 20, 2016);
Gfx.drawText(10, 10, "Test01 PBS - Minimal", 65535);
}