From 543558a02b52d84a40906777f3776123f237a7e3 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Sat, 5 Apr 2025 19:34:50 +0200 Subject: [PATCH] Modified const and extern to always use blocks --- examples/shell/const.q | 12 +++++++----- examples/winapi/winapi.q | 10 ++++++---- lib/core/core_windows.q | 24 ++++++++++++++---------- lib/io/io.q | 14 ++++++++------ lib/mem/alloc_windows.q | 10 ++++++---- lib/mem/const_linux.q | 16 +++++++++------- lib/mem/const_mac.q | 16 +++++++++------- lib/mem/const_windows.q | 16 +++++++++------- lib/mem/free_windows.q | 10 ++++++---- lib/sys/sys_windows.q | 10 ++++++---- lib/thread/thread_linux.q | 22 ++++++++++++---------- lib/thread/thread_windows.q | 10 ++++++---- src/scanner/scanConst.go | 30 ++++++++++++++++++++++++------ src/scanner/scanExtern.go | 32 ++++++++++++++++++++++++++++++-- src/scanner/scanFile.go | 8 ++++---- tests/programs/const.q | 14 ++++++++------ 16 files changed, 164 insertions(+), 90 deletions(-) diff --git a/examples/shell/const.q b/examples/shell/const.q index 9dadaf2..20c1bfa 100644 --- a/examples/shell/const.q +++ b/examples/shell/const.q @@ -1,7 +1,9 @@ -idtype const { - pid 1 -} +const { + idtype { + pid 1 + } -state const { - exited 0x4 + state { + exited 0x4 + } } \ No newline at end of file diff --git a/examples/winapi/winapi.q b/examples/winapi/winapi.q index 702e0cb..5100506 100644 --- a/examples/winapi/winapi.q +++ b/examples/winapi/winapi.q @@ -1,9 +1,11 @@ -user32 extern { - MessageBoxA(window *any, text *byte, title *byte, flags uint) -> int -} - main() { title := "Title." text := "Hi!" user32.MessageBoxA(0, text, title, 0x240040) +} + +extern { + user32 { + MessageBoxA(window *any, text *byte, title *byte, flags uint) -> int + } } \ No newline at end of file diff --git a/lib/core/core_windows.q b/lib/core/core_windows.q index abb6428..088a104 100644 --- a/lib/core/core_windows.q +++ b/lib/core/core_windows.q @@ -1,13 +1,3 @@ -cp const { - utf8 65001 -} - -kernel32 extern { - SetConsoleCP(cp uint) - SetConsoleOutputCP(cp uint) - ExitProcess(code uint) -} - init() { kernel32.SetConsoleCP(cp.utf8) kernel32.SetConsoleOutputCP(cp.utf8) @@ -21,4 +11,18 @@ exit() { crash() { kernel32.ExitProcess(1) +} + +const { + cp { + utf8 65001 + } +} + +extern { + kernel32 { + SetConsoleCP(cp uint) + SetConsoleOutputCP(cp uint) + ExitProcess(code uint) + } } \ No newline at end of file diff --git a/lib/io/io.q b/lib/io/io.q index e05f914..6c83d05 100644 --- a/lib/io/io.q +++ b/lib/io/io.q @@ -1,11 +1,5 @@ import sys -std const { - in 0 - out 1 - err 2 -} - in(buffer []byte) -> int { return sys.read(std.in, buffer, len(buffer)) } @@ -24,4 +18,12 @@ read(fd int, buffer []byte) -> int { write(fd int, buffer []byte) -> int { return sys.write(fd, buffer, len(buffer)) +} + +const { + std { + in 0 + out 1 + err 2 + } } \ No newline at end of file diff --git a/lib/mem/alloc_windows.q b/lib/mem/alloc_windows.q index 2b0c016..c0df727 100644 --- a/lib/mem/alloc_windows.q +++ b/lib/mem/alloc_windows.q @@ -1,7 +1,3 @@ -kernel32 extern { - VirtualAlloc(address int, size uint, flags uint32, protection uint32) -> *any -} - alloc(length int) -> []byte { x := kernel32.VirtualAlloc(0, length+8, mem.commit|mem.reserve, page.readwrite) @@ -11,4 +7,10 @@ alloc(length int) -> []byte { store(x, 8, length) return x + 8 +} + +extern { + kernel32 { + VirtualAlloc(address int, size uint, flags uint32, protection uint32) -> *any + } } \ No newline at end of file diff --git a/lib/mem/const_linux.q b/lib/mem/const_linux.q index 72dd4f4..ca679c9 100644 --- a/lib/mem/const_linux.q +++ b/lib/mem/const_linux.q @@ -1,9 +1,11 @@ -prot const { - read 0x1 - write 0x2 -} +const { + prot { + read 0x1 + write 0x2 + } -map const { - private 0x02 - anonymous 0x20 + map { + private 0x02 + anonymous 0x20 + } } \ No newline at end of file diff --git a/lib/mem/const_mac.q b/lib/mem/const_mac.q index 3f654cc..e7b4123 100644 --- a/lib/mem/const_mac.q +++ b/lib/mem/const_mac.q @@ -1,9 +1,11 @@ -prot const { - read 0x1 - write 0x2 -} +const { + prot { + read 0x1 + write 0x2 + } -map const { - private 0x02 - anonymous 0x1000 + map { + private 0x02 + anonymous 0x1000 + } } \ No newline at end of file diff --git a/lib/mem/const_windows.q b/lib/mem/const_windows.q index acc7f55..45e532f 100644 --- a/lib/mem/const_windows.q +++ b/lib/mem/const_windows.q @@ -1,9 +1,11 @@ -page const { - readwrite 0x0004 -} +const { + page { + readwrite 0x0004 + } -mem const { - commit 0x1000 - reserve 0x2000 - decommit 0x4000 + mem { + commit 0x1000 + reserve 0x2000 + decommit 0x4000 + } } \ No newline at end of file diff --git a/lib/mem/free_windows.q b/lib/mem/free_windows.q index d9bd818..e9f3103 100644 --- a/lib/mem/free_windows.q +++ b/lib/mem/free_windows.q @@ -1,7 +1,9 @@ -kernel32 extern { - VirtualFree(address *any, size uint, type uint32) -> bool -} - free(address []any) { kernel32.VirtualFree(address-8, len(address)+8, mem.decommit) +} + +extern { + kernel32 { + VirtualFree(address *any, size uint, type uint32) -> bool + } } \ No newline at end of file diff --git a/lib/sys/sys_windows.q b/lib/sys/sys_windows.q index d3d3634..5f63de0 100644 --- a/lib/sys/sys_windows.q +++ b/lib/sys/sys_windows.q @@ -10,8 +10,10 @@ write(fd int, buffer *byte, length int) -> int { return length } -kernel32 extern { - GetStdHandle(handle int64) -> int64 - ReadConsole(fd int64, buffer *byte, length uint32, written *uint32) -> bool - WriteConsoleA(fd int64, buffer *byte, length uint32, written *uint32) -> bool +extern { + kernel32 { + GetStdHandle(handle int64) -> int64 + ReadConsole(fd int64, buffer *byte, length uint32, written *uint32) -> bool + WriteConsoleA(fd int64, buffer *byte, length uint32, written *uint32) -> bool + } } \ No newline at end of file diff --git a/lib/thread/thread_linux.q b/lib/thread/thread_linux.q index 7652537..2e3db31 100644 --- a/lib/thread/thread_linux.q +++ b/lib/thread/thread_linux.q @@ -1,16 +1,6 @@ import core import sys -clone const { - vm 0x100 - fs 0x200 - files 0x400 - sighand 0x800 - parent 0x8000 - thread 0x10000 - io 0x80000000 -} - create(func *any) -> int { stack := sys.mmap(0, 4096, 0x1|0x2, 0x02|0x20|0x100) stack += 4096 - 8 @@ -18,4 +8,16 @@ create(func *any) -> int { stack -= 8 store(stack, 8, func) return sys.clone(clone.vm|clone.fs|clone.files|clone.sighand|clone.parent|clone.thread|clone.io, stack, 0, 0, 0) +} + +const { + clone { + vm 0x100 + fs 0x200 + files 0x400 + sighand 0x800 + parent 0x8000 + thread 0x10000 + io 0x80000000 + } } \ No newline at end of file diff --git a/lib/thread/thread_windows.q b/lib/thread/thread_windows.q index dd5136b..f04db03 100644 --- a/lib/thread/thread_windows.q +++ b/lib/thread/thread_windows.q @@ -1,7 +1,9 @@ -kernel32 extern { - CreateThread(attributes int, stackSize int, address *any, parameter int) -> int -} - create(func *any) -> int { return kernel32.CreateThread(0, 4096, func, 0) +} + +extern { + kernel32 { + CreateThread(attributes int, stackSize int, address *any, parameter int) -> int + } } \ No newline at end of file diff --git a/src/scanner/scanConst.go b/src/scanner/scanConst.go index 5a83b0a..d65789d 100644 --- a/src/scanner/scanConst.go +++ b/src/scanner/scanConst.go @@ -9,14 +9,15 @@ import ( // scanConst scans a block of constants. func (s *Scanner) scanConst(file *fs.File, tokens token.List, i int) (int, error) { - groupName := tokens[i].Text(file.Bytes) - i += 2 + i++ if tokens[i].Kind != token.BlockStart { return i, errors.New(errors.MissingBlockStart, file, tokens[i].Position) } i++ + prefix := file.Package + "." + var stack []string for i < len(tokens) { switch tokens[i].Kind { @@ -24,13 +25,30 @@ func (s *Scanner) scanConst(file *fs.File, tokens token.List, i int) (int, error name := tokens[i].Text(file.Bytes) i++ - s.constants <- &core.Constant{ - Name: file.Package + "." + groupName + "." + name, - Token: tokens[i], - File: file, + switch tokens[i].Kind { + case token.Number: + s.constants <- &core.Constant{ + Name: prefix + name, + Token: tokens[i], + File: file, + } + + case token.BlockStart: + stack = append(stack, prefix) + prefix += name + "." + + default: + return i, errors.New(errors.NotImplemented, file, tokens[i].Position) } case token.BlockEnd: + if len(stack) > 0 { + prefix = stack[len(stack)-1] + stack = stack[:len(stack)-1] + i++ + continue + } + return i, nil } diff --git a/src/scanner/scanExtern.go b/src/scanner/scanExtern.go index 140c4ae..ee476b3 100644 --- a/src/scanner/scanExtern.go +++ b/src/scanner/scanExtern.go @@ -6,10 +6,38 @@ import ( "git.urbach.dev/cli/q/src/token" ) -// scanExtern scans a block of external function declarations. +// scanExtern scans a block of external libraries. func (s *Scanner) scanExtern(file *fs.File, tokens token.List, i int) (int, error) { + i++ + + if tokens[i].Kind != token.BlockStart { + return i, errors.New(errors.MissingBlockStart, file, tokens[i].Position) + } + + for i < len(tokens) { + switch tokens[i].Kind { + case token.Identifier: + var err error + i, err = s.scanExternLibrary(file, tokens, i) + + if err != nil { + return i, err + } + + case token.BlockEnd: + return i, nil + } + + i++ + } + + return i, errors.New(errors.MissingBlockEnd, file, tokens[i].Position) +} + +// scanExternLibrary scans a block of external function declarations. +func (s *Scanner) scanExternLibrary(file *fs.File, tokens token.List, i int) (int, error) { dllName := tokens[i].Text(file.Bytes) - i += 2 + i++ if tokens[i].Kind != token.BlockStart { return i, errors.New(errors.MissingBlockStart, file, tokens[i].Position) diff --git a/src/scanner/scanFile.go b/src/scanner/scanFile.go index 234dd6e..2266633 100644 --- a/src/scanner/scanFile.go +++ b/src/scanner/scanFile.go @@ -43,10 +43,6 @@ func (s *Scanner) scanFile(path string, pkg string) error { i, err = s.scanFunction(file, tokens, i) case token.BlockStart: i, err = s.scanStruct(file, tokens, i) - case token.Extern: - i, err = s.scanExtern(file, tokens, i) - case token.Const: - i, err = s.scanConst(file, tokens, i) case token.GroupEnd: return errors.New(errors.MissingGroupStart, file, next.Position) case token.BlockEnd: @@ -56,6 +52,10 @@ func (s *Scanner) scanFile(path string, pkg string) error { default: return errors.New(errors.InvalidDefinition, file, next.Position) } + case token.Const: + i, err = s.scanConst(file, tokens, i) + case token.Extern: + i, err = s.scanExtern(file, tokens, i) case token.Import: i, err = s.scanImport(file, tokens, i) case token.EOF: diff --git a/tests/programs/const.q b/tests/programs/const.q index 5ce99b5..cd40d04 100644 --- a/tests/programs/const.q +++ b/tests/programs/const.q @@ -1,12 +1,14 @@ -num const { - one 1 - two 2 - three 3 -} - main() { assert num.one == 1 assert num.two == 2 assert num.three == 3 assert num.one + num.two == num.three +} + +const { + num { + one 1 + two 2 + three 3 + } } \ No newline at end of file