diff --git a/docs/readme.md b/docs/readme.md index c8d779b..88402ca 100644 --- a/docs/readme.md +++ b/docs/readme.md @@ -114,7 +114,7 @@ The following is a cheat sheet documenting the syntax. | Loop 10 times with a variable | `loop i := 0..10 {}` | ✔️ Stable | | Branch | `if {} else {}` | ✔️ Stable | | Branch multiple times | `switch { cond {} _ {} }` | ✔️ Stable | -| Define a constant | `const { x 42 }` | 🚧 Experimental | +| Define a constant | `const { x = 42 }` | ✔️ Stable | | Declare an external function | `extern { g { f() } }` | ✔️ Stable | | Allocate memory | `mem.alloc(4096)` | ✔️ Stable | | Free memory | `mem.free(buffer)` | 🚧 Experimental | diff --git a/lib/fs/const_linux_arm.q b/lib/fs/const_linux_arm.q index a10a0db..90da053 100644 --- a/lib/fs/const_linux_arm.q +++ b/lib/fs/const_linux_arm.q @@ -1,5 +1,5 @@ const { - _openat 56 - _close 57 - _fstat 80 + _openat = 56 + _close = 57 + _fstat = 80 } \ No newline at end of file diff --git a/lib/fs/const_linux_x86.q b/lib/fs/const_linux_x86.q index 6fada91..f5a8e8b 100644 --- a/lib/fs/const_linux_x86.q +++ b/lib/fs/const_linux_x86.q @@ -1,5 +1,5 @@ const { - _openat 257 - _close 3 - _fstat 5 + _openat = 257 + _close = 3 + _fstat = 5 } \ No newline at end of file diff --git a/lib/fs/const_mac_arm.q b/lib/fs/const_mac_arm.q index 51fbe31..ce4256b 100644 --- a/lib/fs/const_mac_arm.q +++ b/lib/fs/const_mac_arm.q @@ -1,5 +1,5 @@ const { - _open 0x5 - _close 0x6 - _fstat64 0x153 + _open = 0x5 + _close = 0x6 + _fstat64 = 0x153 } \ No newline at end of file diff --git a/lib/fs/const_mac_x86.q b/lib/fs/const_mac_x86.q index 9a35e9d..2bc4dbd 100644 --- a/lib/fs/const_mac_x86.q +++ b/lib/fs/const_mac_x86.q @@ -1,5 +1,5 @@ const { - _open 0x2000005 - _close 0x2000006 - _fstat64 0x2000153 + _open = 0x2000005 + _close = 0x2000006 + _fstat64 = 0x2000153 } \ No newline at end of file diff --git a/lib/fs/const_windows.q b/lib/fs/const_windows.q index 8ec7608..1a7e8bb 100644 --- a/lib/fs/const_windows.q +++ b/lib/fs/const_windows.q @@ -1,6 +1,6 @@ const { - FILE_ATTRIBUTE_NORMAL 0x80 - FILE_SHARE_READ 0x1 - GENERIC_READ 0x80000000 - OPEN_EXISTING 3 + FILE_ATTRIBUTE_NORMAL = 0x80 + FILE_SHARE_READ = 0x1 + GENERIC_READ = 0x80000000 + OPEN_EXISTING = 3 } \ No newline at end of file diff --git a/lib/io/const_linux_arm.q b/lib/io/const_linux_arm.q index 1e9e341..4c0504c 100644 --- a/lib/io/const_linux_arm.q +++ b/lib/io/const_linux_arm.q @@ -1,4 +1,4 @@ const { - _read 63 - _write 64 + _read = 63 + _write = 64 } \ No newline at end of file diff --git a/lib/io/const_linux_x86.q b/lib/io/const_linux_x86.q index 7f45217..9b5035b 100644 --- a/lib/io/const_linux_x86.q +++ b/lib/io/const_linux_x86.q @@ -1,4 +1,4 @@ const { - _read 0 - _write 1 + _read = 0 + _write = 1 } \ No newline at end of file diff --git a/lib/io/const_mac_arm.q b/lib/io/const_mac_arm.q index e4b9163..5118a58 100644 --- a/lib/io/const_mac_arm.q +++ b/lib/io/const_mac_arm.q @@ -1,4 +1,4 @@ const { - _read 0x3 - _write 0x4 + _read = 0x3 + _write = 0x4 } \ No newline at end of file diff --git a/lib/io/const_mac_x86.q b/lib/io/const_mac_x86.q index e8c8102..9f3f3de 100644 --- a/lib/io/const_mac_x86.q +++ b/lib/io/const_mac_x86.q @@ -1,4 +1,4 @@ const { - _read 0x2000003 - _write 0x2000004 + _read = 0x2000003 + _write = 0x2000004 } \ No newline at end of file diff --git a/lib/math/const.q b/lib/math/const.q new file mode 100644 index 0000000..e2a2089 --- /dev/null +++ b/lib/math/const.q @@ -0,0 +1,10 @@ +const { + minInt8 = -128 + maxInt8 = 127 + minInt16 = -32768 + maxInt16 = 32767 + minInt32 = -2147483648 + maxInt32 = 2147483647 + minInt64 = -9223372036854775808 + maxInt64 = 9223372036854775807 +} \ No newline at end of file diff --git a/lib/mem/const_linux.q b/lib/mem/const_linux.q index 0abd02d..c135c3f 100644 --- a/lib/mem/const_linux.q +++ b/lib/mem/const_linux.q @@ -1,6 +1,6 @@ const { - read 0x1 - write 0x2 - private 0x02 - anonymous 0x20 + read = 0x1 + write = 0x2 + private = 0x02 + anonymous = 0x20 } \ No newline at end of file diff --git a/lib/mem/const_linux_arm.q b/lib/mem/const_linux_arm.q index 233a01a..80f0eaa 100644 --- a/lib/mem/const_linux_arm.q +++ b/lib/mem/const_linux_arm.q @@ -1,4 +1,4 @@ const { - _mmap 222 - _munmap 215 + _mmap = 222 + _munmap = 215 } \ No newline at end of file diff --git a/lib/mem/const_linux_x86.q b/lib/mem/const_linux_x86.q index 9c4a2a3..1789781 100644 --- a/lib/mem/const_linux_x86.q +++ b/lib/mem/const_linux_x86.q @@ -1,4 +1,4 @@ const { - _mmap 9 - _munmap 11 + _mmap = 9 + _munmap = 11 } \ No newline at end of file diff --git a/lib/mem/const_mac.q b/lib/mem/const_mac.q index adcc694..21f0973 100644 --- a/lib/mem/const_mac.q +++ b/lib/mem/const_mac.q @@ -1,6 +1,6 @@ const { - read 0x1 - write 0x2 - private 0x02 - anonymous 0x1000 + read = 0x1 + write = 0x2 + private = 0x02 + anonymous = 0x1000 } \ No newline at end of file diff --git a/lib/mem/const_mac_arm.q b/lib/mem/const_mac_arm.q index fcb8159..de0c1e7 100644 --- a/lib/mem/const_mac_arm.q +++ b/lib/mem/const_mac_arm.q @@ -1,4 +1,4 @@ const { - _mmap 0xC5 - _munmap 0x49 + _mmap = 0xC5 + _munmap = 0x49 } \ No newline at end of file diff --git a/lib/mem/const_mac_x86.q b/lib/mem/const_mac_x86.q index 53bdcf4..c3e615e 100644 --- a/lib/mem/const_mac_x86.q +++ b/lib/mem/const_mac_x86.q @@ -1,4 +1,4 @@ const { - _mmap 0x20000C5 - _munmap 0x2000049 + _mmap = 0x20000C5 + _munmap = 0x2000049 } \ No newline at end of file diff --git a/lib/mem/const_windows.q b/lib/mem/const_windows.q index 920201b..a3ff61e 100644 --- a/lib/mem/const_windows.q +++ b/lib/mem/const_windows.q @@ -1,6 +1,6 @@ const { - readwrite 0x0004 - commit 0x1000 - reserve 0x2000 - decommit 0x4000 + readwrite = 0x0004 + commit = 0x1000 + reserve = 0x2000 + decommit = 0x4000 } \ No newline at end of file diff --git a/lib/net/const_linux_arm.q b/lib/net/const_linux_arm.q index 5b98149..8270ad5 100644 --- a/lib/net/const_linux_arm.q +++ b/lib/net/const_linux_arm.q @@ -1,7 +1,7 @@ const { - _close 57 - _socket 198 - _accept 202 - _bind 200 - _listen 201 + _close = 57 + _socket = 198 + _accept = 202 + _bind = 200 + _listen = 201 } \ No newline at end of file diff --git a/lib/net/const_linux_x86.q b/lib/net/const_linux_x86.q index e47f7a5..1a535f3 100644 --- a/lib/net/const_linux_x86.q +++ b/lib/net/const_linux_x86.q @@ -1,7 +1,7 @@ const { - _close 3 - _socket 41 - _accept 43 - _bind 49 - _listen 50 + _close = 3 + _socket = 41 + _accept = 43 + _bind = 49 + _listen = 50 } \ No newline at end of file diff --git a/lib/net/const_mac_arm.q b/lib/net/const_mac_arm.q index 30e3a56..64b72b3 100644 --- a/lib/net/const_mac_arm.q +++ b/lib/net/const_mac_arm.q @@ -1,7 +1,7 @@ const { - _close 0x6 - _socket 0x61 - _accept 0x1E - _bind 0x68 - _listen 0x6A + _close = 0x6 + _socket = 0x61 + _accept = 0x1E + _bind = 0x68 + _listen = 0x6A } \ No newline at end of file diff --git a/lib/net/const_mac_x86.q b/lib/net/const_mac_x86.q index 0cc1807..d427b29 100644 --- a/lib/net/const_mac_x86.q +++ b/lib/net/const_mac_x86.q @@ -1,7 +1,7 @@ const { - _close 0x2000006 - _socket 0x2000061 - _accept 0x200001E - _bind 0x2000068 - _listen 0x200006A + _close = 0x2000006 + _socket = 0x2000061 + _accept = 0x200001E + _bind = 0x2000068 + _listen = 0x200006A } \ No newline at end of file diff --git a/lib/os/const_linux_arm.q b/lib/os/const_linux_arm.q index a6d744e..49f513a 100644 --- a/lib/os/const_linux_arm.q +++ b/lib/os/const_linux_arm.q @@ -1,3 +1,3 @@ const { - _exit 93 + _exit = 93 } \ No newline at end of file diff --git a/lib/os/const_linux_x86.q b/lib/os/const_linux_x86.q index 24c779a..8f015db 100644 --- a/lib/os/const_linux_x86.q +++ b/lib/os/const_linux_x86.q @@ -1,3 +1,3 @@ const { - _exit 60 + _exit = 60 } \ No newline at end of file diff --git a/lib/os/const_mac_arm.q b/lib/os/const_mac_arm.q index e5bbd67..9f88a8f 100644 --- a/lib/os/const_mac_arm.q +++ b/lib/os/const_mac_arm.q @@ -1,3 +1,3 @@ const { - _exit 0x1 + _exit = 0x1 } \ No newline at end of file diff --git a/lib/os/const_mac_x86.q b/lib/os/const_mac_x86.q index d7deda8..509c415 100644 --- a/lib/os/const_mac_x86.q +++ b/lib/os/const_mac_x86.q @@ -1,3 +1,3 @@ const { - _exit 0x2000001 + _exit = 0x2000001 } \ No newline at end of file diff --git a/lib/tcp/const.q b/lib/tcp/const.q index fad63e0..2050caa 100644 --- a/lib/tcp/const.q +++ b/lib/tcp/const.q @@ -1,6 +1,6 @@ const { - backlog 128 - tcp 1 - v4 2 - v6 10 + backlog = 128 + tcp = 1 + v4 = 2 + v6 = 10 } \ No newline at end of file diff --git a/src/scanner/errors.go b/src/scanner/errors.go index eed84d3..bd951a1 100644 --- a/src/scanner/errors.go +++ b/src/scanner/errors.go @@ -12,6 +12,7 @@ var ( InvalidExpression = errors.String("Invalid expression") InvalidFunctionDefinition = errors.String("Invalid function definition") InvalidParameterName = errors.String("Invalid parameter name") + MissingAssign = errors.String("Missing '='") MissingBlockStart = errors.String("Missing '{'") MissingBlockEnd = errors.String("Missing '}'") MissingExpression = errors.String("Missing expression") diff --git a/src/scanner/errors_test.go b/src/scanner/errors_test.go index 095aa74..3d42aff 100644 --- a/src/scanner/errors_test.go +++ b/src/scanner/errors_test.go @@ -28,6 +28,7 @@ var errs = []struct { {"InvalidTopLevel.q", &scanner.InvalidTopLevel{Instruction: "123"}}, {"InvalidTopLevel2.q", &scanner.InvalidTopLevel{Instruction: "\"Hello\""}}, {"InvalidTopLevel3.q", &scanner.InvalidTopLevel{Instruction: "+"}}, + {"MissingAssign.q", scanner.MissingAssign}, {"MissingBlockEnd.q", scanner.MissingBlockEnd}, {"MissingBlockEnd2.q", scanner.MissingBlockEnd}, {"MissingBlockStart.q", scanner.MissingBlockStart}, diff --git a/src/scanner/scanConst.go b/src/scanner/scanConst.go index d2f3340..f54d578 100644 --- a/src/scanner/scanConst.go +++ b/src/scanner/scanConst.go @@ -29,7 +29,12 @@ func (s *scanner) scanConst(file *fs.File, tokens token.List, i int) (int, error case token.NewLine, token.BlockEnd: if start != -1 { name := tokens[start].String(file.Bytes) - valueTokens := tokens[start+1 : i] + + if tokens[start+1].Kind != token.Assign { + return i, errors.New(MissingAssign, file, tokens[start+1].Position) + } + + valueTokens := tokens[start+2 : i] if len(valueTokens) == 0 { return i, errors.New(MissingExpression, file, tokens[start].End()) diff --git a/src/scanner/testdata/errors/InvalidExpression.q b/src/scanner/testdata/errors/InvalidExpression.q index 730492f..06fdbd6 100644 --- a/src/scanner/testdata/errors/InvalidExpression.q +++ b/src/scanner/testdata/errors/InvalidExpression.q @@ -1,3 +1,3 @@ const { - x () + x = () } \ No newline at end of file diff --git a/src/scanner/testdata/errors/MissingAssign.q b/src/scanner/testdata/errors/MissingAssign.q new file mode 100644 index 0000000..8ccf884 --- /dev/null +++ b/src/scanner/testdata/errors/MissingAssign.q @@ -0,0 +1,3 @@ +const { + x +} \ No newline at end of file diff --git a/src/scanner/testdata/errors/MissingExpression.q b/src/scanner/testdata/errors/MissingExpression.q index 8ccf884..82c890a 100644 --- a/src/scanner/testdata/errors/MissingExpression.q +++ b/src/scanner/testdata/errors/MissingExpression.q @@ -1,3 +1,3 @@ const { - x + x = } \ No newline at end of file