Improved type system

This commit is contained in:
Eduard Urbach 2025-02-17 14:31:47 +01:00
parent 3550f9e24e
commit b8e37fafae
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
62 changed files with 189 additions and 172 deletions

View File

@ -5,7 +5,7 @@ main() {
collatz(12)
}
collatz(x Int) {
collatz(x int) {
loop {
if x & 1 == 0 {
x /= 2

View File

@ -4,7 +4,7 @@ main() {
log.number(factorial(5))
}
factorial(x Int) -> Int {
factorial(x int) -> int {
if x <= 1 {
return 1
}

View File

@ -4,7 +4,7 @@ main() {
log.number(fibonacci(10))
}
fibonacci(x Int) -> Int {
fibonacci(x int) -> int {
if x <= 1 {
return x
}

View File

@ -5,7 +5,7 @@ main() {
fizzbuzz(15)
}
fizzbuzz(n Int) {
fizzbuzz(n int) {
x := 1
loop {

View File

@ -4,7 +4,7 @@ main() {
log.number(gcd(1071, 462))
}
gcd(a Int, b Int) -> Int {
gcd(a int, b int) -> int {
loop {
switch {
a == b { return a }

View File

@ -2,8 +2,8 @@ import mem
import sys
struct Point {
x Int
y Int
x int
y int
}
main() {
@ -12,7 +12,7 @@ main() {
delete(p)
}
construct(x Int, y Int) -> *Point {
construct(x int, y int) -> *Point {
p := new(Point)
p.x = x
p.y = y

View File

@ -22,7 +22,7 @@ main() {
}
}
isPrime(x Int) -> Int {
isPrime(x int) -> int {
if x == 2 {
return 1
}

View File

@ -1,5 +1,5 @@
extern user32 {
MessageBoxA(window *Any, text *Int8, title *Int8, type UInt)
MessageBoxA(window *any, text *int8, title *int8, flags uint)
}
main() {

View File

@ -1,7 +1,7 @@
extern kernel32 {
SetConsoleCP(cp UInt)
SetConsoleOutputCP(cp UInt)
ExitProcess(code UInt)
SetConsoleCP(cp uint)
SetConsoleOutputCP(cp uint)
ExitProcess(code uint)
}
const cp {

View File

@ -6,22 +6,22 @@ const std {
err 2
}
in(buffer []Int8) -> Int {
in(buffer []int8) -> int {
return sys.read(std.in, buffer, len(buffer))
}
out(buffer []Int8) -> Int {
out(buffer []int8) -> int {
return sys.write(std.out, buffer, len(buffer))
}
error(buffer []Int8) -> Int {
error(buffer []int8) -> int {
return sys.write(std.err, buffer, len(buffer))
}
read(fd Int, buffer []Int8) -> Int {
read(fd int, buffer []int8) -> int {
return sys.read(fd, buffer, len(buffer))
}
write(fd Int, buffer []Int8) -> Int {
write(fd int, buffer []int8) -> int {
return sys.write(fd, buffer, len(buffer))
}

View File

@ -1,16 +1,15 @@
import mem
import sys
number(x Int) {
length := 20
buffer := mem.alloc(length)
address, count := itoa(x, buffer, length)
number(x int) {
buffer := mem.alloc(20)
address, count := itoa(x, buffer)
sys.write(1, address, count)
mem.free(buffer)
}
itoa(x Int, buffer *Any, length Int) -> (*Any, Int) {
end := buffer + length
itoa(x int, buffer []int8) -> (*any, int) {
end := buffer + len(buffer)
tmp := end
digit := 0

View File

@ -10,7 +10,7 @@ const map {
anonymous 0x20
}
alloc(length Int) -> []Int8 {
alloc(length int) -> []int8 {
x := sys.mmap(0, length+8, prot.read|prot.write, map.private|map.anonymous)
if x < 0x1000 {

View File

@ -10,7 +10,7 @@ const map {
anonymous 0x1000
}
alloc(length Int) -> []Int8 {
alloc(length int) -> []int8 {
x := sys.mmap(0, length+8, prot.read|prot.write, map.private|map.anonymous)
if x < 0x1000 {

View File

@ -9,7 +9,7 @@ const mem {
reserve 0x2000
}
alloc(length Int) -> []Int8 {
alloc(length int) -> []int8 {
x := sys.mmap(0, length+8, page.readwrite, mem.commit|mem.reserve)
if x < 0x1000 {

View File

@ -1,5 +1,5 @@
import sys
free(address []Any) -> Int {
free(address []any) -> int {
return sys.munmap(address-8, len(address)+8)
}

View File

@ -1,3 +1,3 @@
htons(num Int) -> Int {
htons(num int) -> int {
return ((num & 0xFF) << 8) | (num >> 8)
}

View File

@ -1,6 +1,6 @@
import sys
bind(socket Int, port Int) -> Int {
bind(socket int, port int) -> int {
addr := new(sys.sockaddr_in)
addr.sin_family = 2
addr.sin_port = htons(port)

View File

@ -1,6 +1,6 @@
import sys
bind(socket Int, port Int) -> Int {
bind(socket int, port int) -> int {
addr := new(sys.sockaddr_in_bsd)
addr.sin_family = 2
addr.sin_port = htons(port)

View File

@ -1,23 +1,23 @@
getcwd(buffer *Any, length Int) -> Int {
getcwd(buffer *any, length int) -> int {
return syscall(79, buffer, length)
}
chdir(path *Any) -> Int {
chdir(path *any) -> int {
return syscall(80, path)
}
rename(old *Any, new *Any) -> Int {
rename(old *any, new *any) -> int {
return syscall(82, old, new)
}
mkdir(path *Any, mode Int) -> Int {
mkdir(path *any, mode int) -> int {
return syscall(83, path, mode)
}
rmdir(path *Any) -> Int {
rmdir(path *any) -> int {
return syscall(84, path)
}
unlink(file *Any) -> Int {
unlink(file *any) -> int {
return syscall(87, file)
}

View File

@ -1,15 +1,15 @@
read(fd Int, buffer *Any, length Int) -> Int {
read(fd int, buffer *any, length int) -> int {
return syscall(0, fd, buffer, length)
}
write(fd Int, buffer *Any, length Int) -> Int {
write(fd int, buffer *any, length int) -> int {
return syscall(1, fd, buffer, length)
}
open(path *Any, flags Int, mode Int) -> Int {
open(path *any, flags int, mode int) -> int {
return syscall(2, path, flags, mode)
}
close(fd Int) -> Int {
close(fd int) -> int {
return syscall(3, fd)
}

View File

@ -1,15 +1,15 @@
read(fd Int, buffer *Any, length Int) -> Int {
read(fd int, buffer *any, length int) -> int {
return syscall(0x2000003, fd, buffer, length)
}
write(fd Int, buffer *Any, length Int) -> Int {
write(fd int, buffer *any, length int) -> int {
return syscall(0x2000004, fd, buffer, length)
}
open(path *Any, flags Int, mode Int) -> Int {
open(path *any, flags int, mode int) -> int {
return syscall(0x2000005, path, flags, mode)
}
close(fd Int) -> Int {
close(fd int) -> int {
return syscall(0x2000006, fd)
}

View File

@ -1,15 +1,16 @@
extern kernel32 {
GetStdHandle(handle Int) -> Int
WriteConsoleA(fd Int, buffer *Any, length Int, written *Int) -> Bool
ReadFile(fd Int, buffer *Any, length Int) -> Bool
GetStdHandle(handle int64) -> int64
WriteConsoleA(fd int64, buffer *any, length uint32, written *uint32) -> bool
ReadConsole(fd int64, buffer *any, length uint32, written *uint32) -> bool
}
read(fd Int, buffer *Any, length Int) -> Int {
kernel32.ReadFile(fd, buffer, length)
read(fd int64, buffer *any, length int64) -> int64 {
fd = kernel32.GetStdHandle(-10 - fd)
kernel32.ReadConsole(fd, buffer, length, 0)
return length
}
write(fd Int, buffer *Any, length Int) -> Int {
write(fd int64, buffer *any, length int64) -> int64 {
fd = kernel32.GetStdHandle(-10 - fd)
kernel32.WriteConsoleA(fd, buffer, length, 0)
return length

View File

@ -1,7 +1,7 @@
mmap(address Int, length Int, protection Int, flags Int) -> *Any {
mmap(address int, length int, protection int, flags int) -> *any {
return syscall(9, address, length, protection, flags)
}
munmap(address *Any, length Int) -> Int {
munmap(address *any, length int) -> int {
return syscall(11, address, length)
}

View File

@ -1,7 +1,7 @@
mmap(address Int, length Int, protection Int, flags Int) -> *Any {
mmap(address int, length int, protection int, flags int) -> *any {
return syscall(0x20000C5, address, length, protection, flags)
}
munmap(address *Any, length Int) -> Int {
munmap(address *any, length int) -> int {
return syscall(0x2000049, address, length)
}

View File

@ -1,16 +1,16 @@
extern kernel32 {
VirtualAlloc(address Int, length Int, flags Int, protection Int)
VirtualFree(address *Any, length Int, type Int) -> Bool
VirtualAlloc(address int, length int, flags int, protection int)
VirtualFree(address *any, length int, type int) -> bool
}
const mem {
decommit 0x4000
}
mmap(address Int, length Int, protection Int, flags Int) -> *Any {
mmap(address int, length int, protection int, flags int) -> *any {
return kernel32.VirtualAlloc(address, length, flags, protection)
}
munmap(address *Any, length Int) -> Int {
munmap(address *any, length int) -> int {
return kernel32.VirtualFree(address, length, mem.decommit)
}

View File

@ -1,26 +1,26 @@
struct sockaddr_in {
sin_family Int16
sin_port Int16
sin_addr Int64
sin_zero Int64
sin_family int16
sin_port int16
sin_addr int64
sin_zero int64
}
socket(family Int, type Int, protocol Int) -> Int {
socket(family int, type int, protocol int) -> int {
return syscall(41, family, type, protocol)
}
accept(fd Int, address *Any, length Int) -> Int {
accept(fd int, address *any, length int) -> int {
return syscall(43, fd, address, length)
}
bind(fd Int, address *sockaddr_in, length Int) -> Int {
bind(fd int, address *sockaddr_in, length int) -> int {
return syscall(49, fd, address, length)
}
listen(fd Int, backlog Int) -> Int {
listen(fd int, backlog int) -> int {
return syscall(50, fd, backlog)
}
setsockopt(fd Int, level Int, optname Int, optval *Any, optlen Int) -> Int {
setsockopt(fd int, level int, optname int, optval *any, optlen int) -> int {
return syscall(54, fd, level, optname, optval, optlen)
}

View File

@ -1,23 +1,23 @@
struct sockaddr_in_bsd {
sin_len Int8
sin_family Int8
sin_port Int16
sin_addr Int64
sin_zero Int64
sin_len int8
sin_family int8
sin_port int16
sin_addr int64
sin_zero int64
}
socket(family Int, type Int, protocol Int) -> Int {
socket(family int, type int, protocol int) -> int {
return syscall(0x2000061, family, type, protocol)
}
accept(fd Int, address *Any, length Int) -> Int {
accept(fd int, address *any, length int) -> int {
return syscall(0x200001E, fd, address, length)
}
bind(fd Int, address *sockaddr_in_bsd, length Int) -> Int {
bind(fd int, address *sockaddr_in_bsd, length int) -> int {
return syscall(0x2000068, fd, address, length)
}
listen(fd Int, backlog Int) -> Int {
listen(fd int, backlog int) -> int {
return syscall(0x200006A, fd, backlog)
}

View File

@ -1,19 +1,19 @@
clone(flags Int, stack *Any) -> Int {
clone(flags int, stack *any) -> int {
return syscall(56, flags, stack)
}
fork() -> Int {
fork() -> int {
return syscall(57)
}
execve(path *Any, argv *Any, envp *Any) -> Int {
execve(path *any, argv *any, envp *any) -> int {
return syscall(59, path, argv, envp)
}
exit(status Int) {
exit(status int) {
syscall(60, status)
}
waitid(type Int, id Int, info *Any, options Int) -> Int {
waitid(type int, id int, info *any, options int) -> int {
return syscall(247, type, id, info, options)
}

View File

@ -1,15 +1,15 @@
exit(status Int) {
exit(status int) {
syscall(0x2000001, status)
}
fork() -> Int {
fork() -> int {
return syscall(0x2000002)
}
execve(path *Any, argv *Any, envp *Any) -> Int {
execve(path *any, argv *any, envp *any) -> int {
return syscall(0x200003B, path, argv, envp)
}
waitid(type Int, id Int, info *Any, options Int) -> Int {
waitid(type int, id int, info *any, options int) -> int {
return syscall(0x20000AD, type, id, info, options)
}

View File

@ -1,7 +1,7 @@
extern kernel32 {
ExitProcess(code UInt)
ExitProcess(code uint)
}
exit(code Int) {
exit(code int) {
kernel32.ExitProcess(code)
}

View File

@ -1,8 +1,8 @@
struct timespec {
seconds Int
nanoseconds Int
seconds int64
nanoseconds int64
}
nanosleep(duration *timespec) -> Int {
nanosleep(duration *timespec) -> int {
return syscall(35, duration, 0)
}

View File

@ -11,7 +11,7 @@ const clone {
io 0x80000000
}
create(func *Any) -> Int {
create(func *any) -> int {
size := 4096
stack := sys.mmap(0, size, 0x1|0x2, 0x02|0x20|0x100|0x20000)
stack += size

View File

@ -1,7 +1,7 @@
extern kernel32 {
CreateThread(attributes Int, stackSize Int, address *Any, parameter Int) -> Int
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)
}

View File

@ -1,6 +1,6 @@
import sys
sleep(nanoseconds Int) {
sleep(nanoseconds int) {
seconds := 0
if nanoseconds >= 1000000000 {

View File

@ -144,10 +144,6 @@ func (f *Function) ExpressionToRegister(node *expression.Expression, register cp
return nil, err
}
if typ == types.AnyPointer && right.Token.Kind == token.Identifier && f.VariableByName(right.Token.Text(f.File.Bytes)).Type == types.AnyPointer {
typ = types.Int
}
err = f.Execute(node.Token, register, right)
if register != final {
@ -155,5 +151,17 @@ func (f *Function) ExpressionToRegister(node *expression.Expression, register cp
f.FreeRegister(register)
}
_, isArray := typ.(*types.Array)
if isArray {
typ = types.AnyPointer
} else if right.Token.Kind == token.Identifier {
rightVariable := f.VariableByName(right.Token.Text(f.File.Bytes))
if typ == types.AnyPointer && rightVariable.Type == types.AnyPointer {
typ = types.Int
}
}
return typ, err
}

View File

@ -9,10 +9,6 @@ type Array struct {
// Name returns the type name.
func (a *Array) Name() string {
if a.Of == Any {
return "[]Any"
}
return "[]" + a.Of.Name()
}

View File

@ -29,28 +29,36 @@ func ByName(name string, pkg string, structs map[string]*Struct) Type {
}
switch name {
case "Any":
return Any
case "Bool":
return Bool
case "Int":
case "int":
return Int
case "Int64":
case "int64":
return Int64
case "Int32":
case "int32":
return Int32
case "Int16":
case "int16":
return Int16
case "Int8":
case "int8":
return Int8
case "Float":
return Float
case "Float64":
return Float64
case "Float32":
return Float32
case "UInt":
case "uint":
return UInt
case "uint64":
return UInt64
case "uint32":
return UInt32
case "uint16":
return UInt16
case "uint8":
return UInt8
case "float":
return Float
case "float64":
return Float64
case "float32":
return Float32
case "bool":
return Bool
case "any":
return Any
}
typ, exists := structs[pkg+"."+name]

View File

@ -1,15 +1,15 @@
package types
var (
Any = &Base{name: "Any", size: 0}
Any = &Base{name: "any", size: 0}
AnyArray = &Array{Of: Any}
AnyPointer = &Pointer{To: Any}
Int64 = &Base{name: "Int64", size: 8}
Int32 = &Base{name: "Int32", size: 4}
Int16 = &Base{name: "Int16", size: 2}
Int8 = &Base{name: "Int8", size: 1}
Float64 = &Base{name: "Float64", size: 8}
Float32 = &Base{name: "Float32", size: 4}
Int64 = &Base{name: "int64", size: 8}
Int32 = &Base{name: "int32", size: 4}
Int16 = &Base{name: "int16", size: 2}
Int8 = &Base{name: "int8", size: 1}
Float64 = &Base{name: "float64", size: 8}
Float32 = &Base{name: "float32", size: 4}
)
var (
@ -17,4 +17,8 @@ var (
Int = Int64
Float = Float64
UInt = Int
UInt64 = Int64
UInt32 = Int32
UInt16 = Int16
UInt8 = Int8
)

View File

@ -25,5 +25,10 @@ func Is(a Type, b Type) bool {
return true
}
// Temporary hack for implicit casts
if a.Size() > b.Size() {
return true
}
return false
}

View File

@ -7,10 +7,6 @@ type Pointer struct {
// Name returns the type name.
func (p *Pointer) Name() string {
if p.To == nil {
return "*Any"
}
return "*" + p.To.Name()
}

View File

@ -8,12 +8,12 @@ import (
)
func TestName(t *testing.T) {
assert.Equal(t, types.Int.Name(), "Int64")
assert.Equal(t, types.AnyArray.Name(), "[]Any")
assert.Equal(t, types.AnyPointer.Name(), "*Any")
assert.Equal(t, (&types.Pointer{To: types.Int}).Name(), "*Int64")
assert.Equal(t, (&types.Array{Of: types.Int}).Name(), "[]Int64")
assert.Equal(t, types.String.Name(), "[]Int8")
assert.Equal(t, types.Int.Name(), "int64")
assert.Equal(t, types.AnyArray.Name(), "[]any")
assert.Equal(t, types.AnyPointer.Name(), "*any")
assert.Equal(t, (&types.Pointer{To: types.Int}).Name(), "*int64")
assert.Equal(t, (&types.Array{Of: types.Int}).Name(), "[]int64")
assert.Equal(t, types.String.Name(), "[]int8")
}
func TestSize(t *testing.T) {
@ -32,7 +32,7 @@ func TestStruct(t *testing.T) {
s := types.NewStruct("main", "Test")
assert.Equal(t, s.Name(), "Test")
assert.Equal(t, s.Size(), 0)
field := &types.Field{Name: "TestField", TypeName: "Int8"}
field := &types.Field{Name: "TestField", TypeName: "int8"}
s.AddField(field)
s.Update(nil)
assert.Equal(t, s.Size(), 1)

View File

@ -1 +1 @@
f(a Int,) -> Int { return a }
f(a int,) -> int { return a }

View File

@ -1 +1 @@
f(,a Int) {}
f(,a int) {}

View File

@ -2,6 +2,6 @@ main() {
writeToMemory(42)
}
writeToMemory(p *Any) {
writeToMemory(p *any) {
p[0] = 'A'
}

View File

@ -2,6 +2,6 @@ main() {
x := 1 + f(x)
}
f(x Int) -> Int {
f(x int) -> int {
return x
}

View File

@ -49,8 +49,8 @@ var errs = []struct {
{"MissingParameter3.q", errors.MissingParameter},
{"MissingType.q", errors.MissingType},
{"ReturnCountMismatch.q", &errors.ReturnCountMismatch{Count: 1, ExpectedCount: 0}},
{"TypeMismatch.q", &errors.TypeMismatch{Expected: "*Any", Encountered: "Int64", ParameterName: "p"}},
{"TypeMismatch2.q", &errors.TypeMismatch{Expected: "[]Any", Encountered: "Int64", ParameterName: "array"}},
{"TypeMismatch.q", &errors.TypeMismatch{Expected: "*any", Encountered: "int64", ParameterName: "p"}},
{"TypeMismatch2.q", &errors.TypeMismatch{Expected: "[]any", Encountered: "int64", ParameterName: "array"}},
{"UnknownFunction.q", &errors.UnknownFunction{Name: "unknown"}},
{"UnknownFunction2.q", &errors.UnknownFunction{Name: "f"}},
{"UnknownIdentifier.q", &errors.UnknownIdentifier{Name: "x"}},

View File

@ -12,7 +12,7 @@ main() {
assert b == 5
}
f(b Int) -> (Int, Int) {
f(b int) -> (int, int) {
a := 0
if b >= 10 {

View File

@ -74,10 +74,10 @@ main() {
sys.exit(1)
}
inc(x Int) -> Int {
inc(x int) -> int {
return x + 1
}
dec(x Int) -> Int {
dec(x int) -> int {
return x - 1
}

View File

@ -2,6 +2,6 @@ main() {
assert f(1) + f(2) + f(3) == 9
}
f(x Int) -> Int {
f(x int) -> int {
return x + 1
}

View File

@ -12,6 +12,6 @@ main() {
}
}
f(x Int) -> Int {
f(x int) -> int {
return x
}

View File

@ -4,10 +4,10 @@ main() {
assert result == 10
}
div(x Int, y Int) -> Int {
div(x int, y int) -> int {
return x / y
}
div10(x Int) -> Int {
div10(x int) -> int {
return x / 10
}

View File

@ -5,6 +5,6 @@ main() {
assert neg(256) == -256
}
neg(x Int) -> Int {
neg(x int) -> int {
return -x
}

View File

@ -2,6 +2,6 @@ main() {
assert f(f(f(1))) == 4
}
f(x Int) -> Int {
f(x int) -> int {
return x + 1
}

View File

@ -2,7 +2,7 @@ main() {
f(10)
}
f(new Int) {
f(new int) {
old := new
new -= 1
assert new != old

View File

@ -2,11 +2,11 @@ main() {
assert f(1, 2, 3) == 21
}
f(x Int, y Int, z Int) -> Int {
f(x int, y int, z int) -> int {
w := g(4, 5, 6)
return x + y + z + w
}
g(x Int, y Int, z Int) -> Int {
g(x int, y int, z int) -> int {
return x + y + z
}

View File

@ -2,11 +2,11 @@ main() {
f1(1, 2, 3, 4, 5, 6)
}
f1(a Int, b Int, c Int, d Int, e Int, f Int) {
f1(a int, b int, c int, d int, e int, f int) {
f2(f, e, d, c, b, a)
}
f2(a Int, b Int, c Int, d Int, e Int, f Int) {
f2(a int, b int, c int, d int, e int, f int) {
assert a == 6
assert b == 5
assert c == 4

View File

@ -2,11 +2,11 @@ main() {
assert f(1) == 3
}
f(x Int) -> Int {
f(x int) -> int {
y := g()
return x + y
}
g() -> Int {
g() -> int {
return 2
}

View File

@ -15,14 +15,14 @@ main() {
assert i == 1 + 4
}
reverse2(a Int, b Int) -> (Int, Int) {
reverse2(a int, b int) -> (int, int) {
return b, a
}
reverse3(a Int, b Int, c Int) -> (Int, Int, Int) {
reverse3(a int, b int, c int) -> (int, int, int) {
return c, b, a
}
mix4(a Int, b Int, c Int, d Int) -> (Int, Int, Int, Int) {
mix4(a int, b int, c int, d int) -> (int, int, int, int) {
return d + a, c + b, b + c, a + d
}

View File

@ -2,10 +2,10 @@ main() {
assert f(2) == 6
}
f(x Int) -> Int {
f(x int) -> int {
return x + 1 + g(x)
}
g(x Int) -> Int {
g(x int) -> int {
return x + 1
}

View File

@ -2,6 +2,6 @@ main() {
assert f(1) == 3
}
f(x Int) -> Int {
f(x int) -> int {
return x + 1 + x
}

View File

@ -2,6 +2,6 @@ main() {
assert f(2, 3) == 25
}
f(x Int, y Int) -> Int {
f(x int, y int) -> int {
return (x + y) * (x + y)
}

View File

@ -1,6 +1,6 @@
struct Point {
x Int
y Int
x int
y int
}
main() {