...
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
set -e
|
||||
|
||||
pushd core
|
||||
dotnet run --project ../../compiler print.nub --type=lib
|
||||
dotnet run --project ../../compiler sys.nub print.nub file.nub --type=lib
|
||||
popd
|
||||
|
||||
pushd program
|
||||
|
||||
24
examples/core/file.nub
Normal file
24
examples/core/file.nub
Normal file
@@ -0,0 +1,24 @@
|
||||
module file
|
||||
|
||||
export func read_text(path: string): ^u8 {
|
||||
let fd = sys::open(path.ptr 0 0o644)
|
||||
if fd < 0 {
|
||||
return ^u8(0) // failed to open
|
||||
}
|
||||
|
||||
let buf_size: u64 = 4096
|
||||
let buf = c::malloc(buf_size) as ^u8
|
||||
if buf == ^u8(0) {
|
||||
return ^u8(0)
|
||||
}
|
||||
|
||||
let bytes_read = sys::read(fd, buf, buf_size)
|
||||
if bytes_read < 0 {
|
||||
c::free(buf)
|
||||
return ^u8(0)
|
||||
}
|
||||
|
||||
buf[bytes_read] = 0
|
||||
|
||||
return buf
|
||||
}
|
||||
@@ -1,7 +1,10 @@
|
||||
module core
|
||||
|
||||
extern func puts(text: ^u8)
|
||||
|
||||
export func print(text: string) {
|
||||
puts(text.ptr)
|
||||
sys::write(0 text.ptr text.length)
|
||||
}
|
||||
|
||||
export func println(text: string) {
|
||||
print(text)
|
||||
print("\n")
|
||||
}
|
||||
5
examples/core/sys.nub
Normal file
5
examples/core/sys.nub
Normal file
@@ -0,0 +1,5 @@
|
||||
module sys
|
||||
|
||||
export extern func read(fd: u32 buf: ^u8 count: u64): i64
|
||||
export extern func write(fd: u32 buf: ^u8 count: u64): i64
|
||||
export extern func open(fileName: ^u8 flags: i32 mode: u16): i64
|
||||
@@ -2,24 +2,11 @@ module main
|
||||
|
||||
extern func puts(text: ^u8)
|
||||
|
||||
export func print(text: string) {
|
||||
puts(text.ptr)
|
||||
}
|
||||
|
||||
enum Message {
|
||||
Tell: string
|
||||
}
|
||||
|
||||
func main(): i32 {
|
||||
let x = "test"
|
||||
core::println("Hello, world!")
|
||||
|
||||
let y = {
|
||||
abc = x
|
||||
}
|
||||
|
||||
let a: Message = enum Message::Tell x
|
||||
|
||||
core::print(x)
|
||||
let file = file::read_text("file.nub")
|
||||
puts(file)
|
||||
|
||||
return 0
|
||||
}
|
||||
Reference in New Issue
Block a user