Skip to content

Commit

Permalink
Add printString support to legacy C backend
Browse files Browse the repository at this point in the history
  • Loading branch information
paulcadman committed Jan 4, 2023
1 parent 64f1265 commit fce2aae
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 10 deletions.
4 changes: 4 additions & 0 deletions c-runtime/standalone/c-runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ prim_io prim_printNat(prim_nat n) {
exit(1);
}

prim_io prim_printString(prim_string s) {
exit(1);
}

// Tries to parse str as a positive integer.
// Returns -1 if parsing fails.
int parsePositiveInt(char *str) {
Expand Down
6 changes: 5 additions & 1 deletion c-runtime/wasi-libc/c-runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,11 @@ int debug(const char* str) {
}

prim_io prim_printNat(prim_nat n) {
return putStrLn(intToStr(n));
return putStr(intToStr(n));
}

prim_io prim_printString(prim_string s) {
return putStr(s);
}

#endif // C_RUNTIME_H_
6 changes: 5 additions & 1 deletion c-runtime/wasi-standalone/c-runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,11 @@ int debug(const char* str) {
}

prim_io prim_printNat(prim_nat n) {
return putStrLn(intToStr(n));
return putStr(intToStr(n));
}

prim_io prim_printString(prim_string s) {
return putStr(s);
}

// Tries to parse str as a positive integer.
Expand Down
1 change: 1 addition & 0 deletions test/BackendC/Positive.hs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ tests =
PosTest "Mutually recursive function" $(mkRelDir "MutuallyRecursive") (WASI StdlibExclude),
PosTest "Nested List type" $(mkRelDir "NestedList") (WASI StdlibExclude),
PosTest "Builtin types and functions" $(mkRelDir "Builtins") (WASI StdlibExclude),
PosTest "Builtin types and functions from stdlib" $(mkRelDir "BuiltinsStdlib") (WASI StdlibInclude),
PosTest "Import from embedded standard library" $(mkRelDir "StdlibImport") (WASI StdlibInclude),
PosTest "Axiom without a compile block" $(mkRelDir "AxiomNoCompile") (WASI StdlibInclude),
PosTest "Invoke a function using exported name" $(mkRelDir "ExportName") (WASM (WASMInfo (actualCallExport "fun" []))),
Expand Down
5 changes: 5 additions & 0 deletions test/Internal/Eval/Positive.hs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ tests =
$(mkRelDir ".")
$(mkRelFile "LitString.juvix")
$(mkRelFile "out/LitString.out"),
PosTest
"Literal String IO"
$(mkRelDir ".")
$(mkRelFile "LitStringIO.juvix")
$(mkRelFile "out/LitStringIO.out"),
PosTest
"Mutually defined functions"
$(mkRelDir ".")
Expand Down
Binary file added tests/Internal/positive/LitStringIO
Binary file not shown.
8 changes: 8 additions & 0 deletions tests/Internal/positive/LitStringIO.juvix
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module LitStringIO;

open import Stdlib.Prelude;

main : IO;
main := printStringLn "hello";

end;
1 change: 1 addition & 0 deletions tests/Internal/positive/out/LitStringIO.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello
21 changes: 13 additions & 8 deletions tests/positive/MiniC/Builtins/Input.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,24 @@ infix 4 ==;
== _ _ := false;

builtin IO axiom IO : Type;
builtin string axiom String : Type;

infixl 1 >>;
builtin IO-sequence axiom >> : IO → IO → IO;
builtin nat-print axiom printNat : ℕ → IO;
builtin string-print axiom printString : String → IO;

printNatLn : ℕ -> IO;
printNatLn n := printNat n >> printString "\n";

main : IO;
main := printNat (boolToNat true)
>> printNat (boolToNat false)
>> printNat (mult 3 (2 + 2))
>> printNat 2
>> printNat (if (1 == 2) 100 200)
>> printNat (if (1 == 1) 300 400)
>> if (1 == 2) (printNat 500) (printNat 600)
>> if (1 == 1) (printNat 700) (printNat 800);
main := printNatLn (boolToNat true)
>> printNatLn (boolToNat false)
>> printNatLn (mult 3 (2 + 2))
>> printNatLn 2
>> printNatLn (if (1 == 2) 100 200)
>> printNatLn (if (1 == 1) 300 400)
>> if (1 == 2) (printNatLn 500) (printNatLn 600)
>> if (1 == 1) (printNatLn 700) (printNatLn 800);

end;
24 changes: 24 additions & 0 deletions tests/positive/MiniC/BuiltinsStdlib/Input.juvix
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module Input;

open import Stdlib.Prelude;
open import Stdlib.Data.Nat.Ord;

boolToNat : Bool → Nat;
boolToNat false := zero;
boolToNat true := suc zero;

mult : Nat → Nat → Nat;
mult zero _ := zero;
mult (suc n) m := m + (mult n m);

main : IO;
main := printNatLn (boolToNat true)
>> printNatLn (boolToNat false)
>> printNatLn (mult 3 (2 + 2))
>> printNatLn 2
>> printNatLn (if (1 == 2) 100 200)
>> printNatLn (if (1 == 1) 300 400)
>> if (1 == 2) (printNatLn 500) (printNatLn 600)
>> if (1 == 1) (printNatLn 700) (printNatLn 800);

end;
8 changes: 8 additions & 0 deletions tests/positive/MiniC/BuiltinsStdlib/expected.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
1
0
12
2
200
300
600
700
Empty file.

0 comments on commit fce2aae

Please sign in to comment.