Modified const and extern to always use blocks
This commit is contained in:
@ -1,7 +1,9 @@
|
|||||||
idtype const {
|
const {
|
||||||
|
idtype {
|
||||||
pid 1
|
pid 1
|
||||||
}
|
}
|
||||||
|
|
||||||
state const {
|
state {
|
||||||
exited 0x4
|
exited 0x4
|
||||||
}
|
}
|
||||||
|
}
|
@ -1,9 +1,11 @@
|
|||||||
user32 extern {
|
|
||||||
MessageBoxA(window *any, text *byte, title *byte, flags uint) -> int
|
|
||||||
}
|
|
||||||
|
|
||||||
main() {
|
main() {
|
||||||
title := "Title."
|
title := "Title."
|
||||||
text := "Hi!"
|
text := "Hi!"
|
||||||
user32.MessageBoxA(0, text, title, 0x240040)
|
user32.MessageBoxA(0, text, title, 0x240040)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extern {
|
||||||
|
user32 {
|
||||||
|
MessageBoxA(window *any, text *byte, title *byte, flags uint) -> int
|
||||||
|
}
|
||||||
|
}
|
@ -1,13 +1,3 @@
|
|||||||
cp const {
|
|
||||||
utf8 65001
|
|
||||||
}
|
|
||||||
|
|
||||||
kernel32 extern {
|
|
||||||
SetConsoleCP(cp uint)
|
|
||||||
SetConsoleOutputCP(cp uint)
|
|
||||||
ExitProcess(code uint)
|
|
||||||
}
|
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
kernel32.SetConsoleCP(cp.utf8)
|
kernel32.SetConsoleCP(cp.utf8)
|
||||||
kernel32.SetConsoleOutputCP(cp.utf8)
|
kernel32.SetConsoleOutputCP(cp.utf8)
|
||||||
@ -22,3 +12,17 @@ exit() {
|
|||||||
crash() {
|
crash() {
|
||||||
kernel32.ExitProcess(1)
|
kernel32.ExitProcess(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const {
|
||||||
|
cp {
|
||||||
|
utf8 65001
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extern {
|
||||||
|
kernel32 {
|
||||||
|
SetConsoleCP(cp uint)
|
||||||
|
SetConsoleOutputCP(cp uint)
|
||||||
|
ExitProcess(code uint)
|
||||||
|
}
|
||||||
|
}
|
14
lib/io/io.q
14
lib/io/io.q
@ -1,11 +1,5 @@
|
|||||||
import sys
|
import sys
|
||||||
|
|
||||||
std const {
|
|
||||||
in 0
|
|
||||||
out 1
|
|
||||||
err 2
|
|
||||||
}
|
|
||||||
|
|
||||||
in(buffer []byte) -> int {
|
in(buffer []byte) -> int {
|
||||||
return sys.read(std.in, buffer, len(buffer))
|
return sys.read(std.in, buffer, len(buffer))
|
||||||
}
|
}
|
||||||
@ -25,3 +19,11 @@ read(fd int, buffer []byte) -> int {
|
|||||||
write(fd int, buffer []byte) -> int {
|
write(fd int, buffer []byte) -> int {
|
||||||
return sys.write(fd, buffer, len(buffer))
|
return sys.write(fd, buffer, len(buffer))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const {
|
||||||
|
std {
|
||||||
|
in 0
|
||||||
|
out 1
|
||||||
|
err 2
|
||||||
|
}
|
||||||
|
}
|
@ -1,7 +1,3 @@
|
|||||||
kernel32 extern {
|
|
||||||
VirtualAlloc(address int, size uint, flags uint32, protection uint32) -> *any
|
|
||||||
}
|
|
||||||
|
|
||||||
alloc(length int) -> []byte {
|
alloc(length int) -> []byte {
|
||||||
x := kernel32.VirtualAlloc(0, length+8, mem.commit|mem.reserve, page.readwrite)
|
x := kernel32.VirtualAlloc(0, length+8, mem.commit|mem.reserve, page.readwrite)
|
||||||
|
|
||||||
@ -12,3 +8,9 @@ alloc(length int) -> []byte {
|
|||||||
store(x, 8, length)
|
store(x, 8, length)
|
||||||
return x + 8
|
return x + 8
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extern {
|
||||||
|
kernel32 {
|
||||||
|
VirtualAlloc(address int, size uint, flags uint32, protection uint32) -> *any
|
||||||
|
}
|
||||||
|
}
|
@ -1,9 +1,11 @@
|
|||||||
prot const {
|
const {
|
||||||
|
prot {
|
||||||
read 0x1
|
read 0x1
|
||||||
write 0x2
|
write 0x2
|
||||||
}
|
}
|
||||||
|
|
||||||
map const {
|
map {
|
||||||
private 0x02
|
private 0x02
|
||||||
anonymous 0x20
|
anonymous 0x20
|
||||||
}
|
}
|
||||||
|
}
|
@ -1,9 +1,11 @@
|
|||||||
prot const {
|
const {
|
||||||
|
prot {
|
||||||
read 0x1
|
read 0x1
|
||||||
write 0x2
|
write 0x2
|
||||||
}
|
}
|
||||||
|
|
||||||
map const {
|
map {
|
||||||
private 0x02
|
private 0x02
|
||||||
anonymous 0x1000
|
anonymous 0x1000
|
||||||
}
|
}
|
||||||
|
}
|
@ -1,9 +1,11 @@
|
|||||||
page const {
|
const {
|
||||||
|
page {
|
||||||
readwrite 0x0004
|
readwrite 0x0004
|
||||||
}
|
}
|
||||||
|
|
||||||
mem const {
|
mem {
|
||||||
commit 0x1000
|
commit 0x1000
|
||||||
reserve 0x2000
|
reserve 0x2000
|
||||||
decommit 0x4000
|
decommit 0x4000
|
||||||
}
|
}
|
||||||
|
}
|
@ -1,7 +1,9 @@
|
|||||||
kernel32 extern {
|
|
||||||
VirtualFree(address *any, size uint, type uint32) -> bool
|
|
||||||
}
|
|
||||||
|
|
||||||
free(address []any) {
|
free(address []any) {
|
||||||
kernel32.VirtualFree(address-8, len(address)+8, mem.decommit)
|
kernel32.VirtualFree(address-8, len(address)+8, mem.decommit)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extern {
|
||||||
|
kernel32 {
|
||||||
|
VirtualFree(address *any, size uint, type uint32) -> bool
|
||||||
|
}
|
||||||
|
}
|
@ -10,8 +10,10 @@ write(fd int, buffer *byte, length int) -> int {
|
|||||||
return length
|
return length
|
||||||
}
|
}
|
||||||
|
|
||||||
kernel32 extern {
|
extern {
|
||||||
|
kernel32 {
|
||||||
GetStdHandle(handle int64) -> int64
|
GetStdHandle(handle int64) -> int64
|
||||||
ReadConsole(fd int64, buffer *byte, length uint32, written *uint32) -> bool
|
ReadConsole(fd int64, buffer *byte, length uint32, written *uint32) -> bool
|
||||||
WriteConsoleA(fd int64, buffer *byte, length uint32, written *uint32) -> bool
|
WriteConsoleA(fd int64, buffer *byte, length uint32, written *uint32) -> bool
|
||||||
}
|
}
|
||||||
|
}
|
@ -1,16 +1,6 @@
|
|||||||
import core
|
import core
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
clone const {
|
|
||||||
vm 0x100
|
|
||||||
fs 0x200
|
|
||||||
files 0x400
|
|
||||||
sighand 0x800
|
|
||||||
parent 0x8000
|
|
||||||
thread 0x10000
|
|
||||||
io 0x80000000
|
|
||||||
}
|
|
||||||
|
|
||||||
create(func *any) -> int {
|
create(func *any) -> int {
|
||||||
stack := sys.mmap(0, 4096, 0x1|0x2, 0x02|0x20|0x100)
|
stack := sys.mmap(0, 4096, 0x1|0x2, 0x02|0x20|0x100)
|
||||||
stack += 4096 - 8
|
stack += 4096 - 8
|
||||||
@ -19,3 +9,15 @@ create(func *any) -> int {
|
|||||||
store(stack, 8, func)
|
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)
|
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
|
||||||
|
}
|
||||||
|
}
|
@ -1,7 +1,9 @@
|
|||||||
kernel32 extern {
|
|
||||||
CreateThread(attributes int, stackSize int, address *any, parameter int) -> int
|
|
||||||
}
|
|
||||||
|
|
||||||
create(func *any) -> int {
|
create(func *any) -> int {
|
||||||
return kernel32.CreateThread(0, 4096, func, 0)
|
return kernel32.CreateThread(0, 4096, func, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extern {
|
||||||
|
kernel32 {
|
||||||
|
CreateThread(attributes int, stackSize int, address *any, parameter int) -> int
|
||||||
|
}
|
||||||
|
}
|
@ -9,14 +9,15 @@ import (
|
|||||||
|
|
||||||
// scanConst scans a block of constants.
|
// scanConst scans a block of constants.
|
||||||
func (s *Scanner) scanConst(file *fs.File, tokens token.List, i int) (int, error) {
|
func (s *Scanner) scanConst(file *fs.File, tokens token.List, i int) (int, error) {
|
||||||
groupName := tokens[i].Text(file.Bytes)
|
i++
|
||||||
i += 2
|
|
||||||
|
|
||||||
if tokens[i].Kind != token.BlockStart {
|
if tokens[i].Kind != token.BlockStart {
|
||||||
return i, errors.New(errors.MissingBlockStart, file, tokens[i].Position)
|
return i, errors.New(errors.MissingBlockStart, file, tokens[i].Position)
|
||||||
}
|
}
|
||||||
|
|
||||||
i++
|
i++
|
||||||
|
prefix := file.Package + "."
|
||||||
|
var stack []string
|
||||||
|
|
||||||
for i < len(tokens) {
|
for i < len(tokens) {
|
||||||
switch tokens[i].Kind {
|
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)
|
name := tokens[i].Text(file.Bytes)
|
||||||
i++
|
i++
|
||||||
|
|
||||||
|
switch tokens[i].Kind {
|
||||||
|
case token.Number:
|
||||||
s.constants <- &core.Constant{
|
s.constants <- &core.Constant{
|
||||||
Name: file.Package + "." + groupName + "." + name,
|
Name: prefix + name,
|
||||||
Token: tokens[i],
|
Token: tokens[i],
|
||||||
File: file,
|
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:
|
case token.BlockEnd:
|
||||||
|
if len(stack) > 0 {
|
||||||
|
prefix = stack[len(stack)-1]
|
||||||
|
stack = stack[:len(stack)-1]
|
||||||
|
i++
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
return i, nil
|
return i, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,10 +6,38 @@ import (
|
|||||||
"git.urbach.dev/cli/q/src/token"
|
"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) {
|
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)
|
dllName := tokens[i].Text(file.Bytes)
|
||||||
i += 2
|
i++
|
||||||
|
|
||||||
if tokens[i].Kind != token.BlockStart {
|
if tokens[i].Kind != token.BlockStart {
|
||||||
return i, errors.New(errors.MissingBlockStart, file, tokens[i].Position)
|
return i, errors.New(errors.MissingBlockStart, file, tokens[i].Position)
|
||||||
|
@ -43,10 +43,6 @@ func (s *Scanner) scanFile(path string, pkg string) error {
|
|||||||
i, err = s.scanFunction(file, tokens, i)
|
i, err = s.scanFunction(file, tokens, i)
|
||||||
case token.BlockStart:
|
case token.BlockStart:
|
||||||
i, err = s.scanStruct(file, tokens, i)
|
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:
|
case token.GroupEnd:
|
||||||
return errors.New(errors.MissingGroupStart, file, next.Position)
|
return errors.New(errors.MissingGroupStart, file, next.Position)
|
||||||
case token.BlockEnd:
|
case token.BlockEnd:
|
||||||
@ -56,6 +52,10 @@ func (s *Scanner) scanFile(path string, pkg string) error {
|
|||||||
default:
|
default:
|
||||||
return errors.New(errors.InvalidDefinition, file, next.Position)
|
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:
|
case token.Import:
|
||||||
i, err = s.scanImport(file, tokens, i)
|
i, err = s.scanImport(file, tokens, i)
|
||||||
case token.EOF:
|
case token.EOF:
|
||||||
|
@ -1,12 +1,14 @@
|
|||||||
num const {
|
|
||||||
one 1
|
|
||||||
two 2
|
|
||||||
three 3
|
|
||||||
}
|
|
||||||
|
|
||||||
main() {
|
main() {
|
||||||
assert num.one == 1
|
assert num.one == 1
|
||||||
assert num.two == 2
|
assert num.two == 2
|
||||||
assert num.three == 3
|
assert num.three == 3
|
||||||
assert num.one + num.two == num.three
|
assert num.one + num.two == num.three
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const {
|
||||||
|
num {
|
||||||
|
one 1
|
||||||
|
two 2
|
||||||
|
three 3
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user