Skip to content

Commit

Permalink
more concrete syntax examples
Browse files Browse the repository at this point in the history
  • Loading branch information
robertmuth committed Jun 1, 2024
1 parent dd1ca38 commit d5bdd54
Show file tree
Hide file tree
Showing 6 changed files with 583 additions and 0 deletions.
130 changes: 130 additions & 0 deletions FrontEnd/ConcreteSyntax/LangTest/array_test.cw
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
module main:

import test

type type_array = [3]bool

type type_slice = slice(s32)

global c1 = [10]s32{1, 2, 3}

global! c2 = [10]s32{1, 2, 3}

-- (let c20 auto (len c1)")
-- "(let c21 auto (at c1 2))")
global dim = 5_u16

fun foo(a [10]u8, b [dim]u64) u8:
let v2 = c1[0]
let v3 = &c1[0]
let v4 = &!c2[0]
set c2[0] = 666
return 66

fun update_array(s slice!(u8), pos uint, new u8) u8:
let old = s[pos]
set s[pos] = new
return old

-- ERROR: (let f4 (slice mut s32) e1)
fun baz() void:
-- ERROR: (= (at c1 5) 0)
let pc1 ^s32 = front(c1)
set c2[5] = 0

@pub rec type_rec3:
u2 u16
u3 u64
u5 [10]u8
u6 u64

global! r1 = type_rec3{u5 : [10]u8{77, 88, 99}}

global! c4 = [10]u8{41, 51, 61}

fun test_mixed_array() void:
--
@ref let! a = [10]u8{1, 2, 3}
let pa = &a
let pa_mut = &!a
test::AssertEq#(c4[2], 61_u8)
set c4 = a
test::AssertEq#(c4[2], 3_u8)
set c4[2] = 4_u8
set a = c4
test::AssertEq#(a[2], 4_u8)
test::AssertEq#(r1.u5[1], 88_u8)
set r1.u5 = a
test::AssertEq#(r1.u5[1], 2_u8)
set r1.u5[1] = 111
set a = r1.u5
test::AssertEq#(a[1], 111_u8)

fun test_local_array() void:
--
@ref let! a = [10]u8{1, 2, 3}
@ref let b = [10]u8{4, 5, 6}
let pa = &a
let pa_mut = &!a
let pb = &b
test::AssertEq#(a[0], 1_u8)
test::AssertEq#(b[2], 6_u8)
set a[0] = 6
test::AssertEq#(a[0], 6_u8)
set pa_mut^[2] = 77_u8
test::AssertEq#(pa^[2], 77_u8)
test::AssertEq#(pa_mut^[2], 77_u8)
test::AssertEq#(pb^[0], 4_u8)
set pa_mut^[0] = 66
test::AssertEq#(a[0], 66_u8)
set a = b
test::AssertEq#(a[0], 4_u8)
test::AssertEq#(update_array(a, 0, 2), 4_u8)
test::AssertEq#(update_array(a, 0, 3), 2_u8)
test::AssertEq#(update_array(pa_mut^, 0, 2), 3_u8)

global d1 = [10]s32{11, 22, 33}

global! d2 = [10]s32{111, 222, 333}

global! c3 = [10]u8{4, 5, 6}

global e1 slice(s32) = d1

global e2 slice!(s32) = d2

global e3 = [5]s32{0, 1, 2, 3, 4}

global e4 = [2]slice(s32){e1, e1}

-- ERROR
-- (global e5 (slice (slice s32)) e4)
-- (global e3 (slice mut s32) d2)
global f1 slice(s32) = e1

global f3 slice(s32) = e2

global f2 slice!(s32) = e2

fun test_global_array() void:
-- basic
test::AssertEq#(c1[1], 2_s32)
test::AssertEq#(c2[2], 3_s32)
test::AssertEq#(e1[1], 22_s32)
test::AssertEq#(e2[2], 333_s32)
test::AssertEq#(f1[1], 22_s32)
test::AssertEq#(f2[2], 333_s32)
test::AssertEq#(f3[0], 111_s32)
-- basic
test::AssertEq#(c3[0], 4_u8)
test::AssertEq#(update_array(c3, 0, 77), 4_u8)
test::AssertEq#(update_array(c3, 0, 5), 77_u8)
test::AssertEq#(len(e1), 10_uint)

@cdecl fun main(argc s32, argv ^^u8) s32:
shed test_global_array()
shed test_local_array()
shed test_mixed_array()
-- test end
test::Success#()
return 0
85 changes: 85 additions & 0 deletions FrontEnd/ConcreteSyntax/LangTest/assign_test.cw
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
module main:

import test

@pub rec type_rec1:
-- this is a comment with \" with quotes \t
i1 s64
i2 u64
i3 s32
i4 u32
i5 s16
i6 u16
i7 s8
i8 u8
f1 r64
f2 r32
b1 bool
a1 [7]u8
a2 [7]u16
a3 [7]u32
a4 [7]u64
a5 [7]r32
a6 [7]r64

@pub rec type_rec2:
t1 bool
t2 u32
t3 type_rec1
t4 bool

@pub rec type_rec3:
u2 u16
u3 u64
u4 type_rec2
u5 [13]u16
u6 u64

global! ga1 [5]s64 = undef

global! gr1 type_rec1 = undef

global! gar1 [5]type_rec1 = undef

global! gr2 type_rec2 = undef

global! gar2 [5]type_rec2 = undef

fun get_addr() ^!type_rec1:
return &!gr1

@cdecl fun main(argc s32, argv ^^u8) s32:
-- a1 u32
set ga1[3] = 0x8765432187654321
test::AssertEq#(ga1[3], 0x8765432187654321_s64)
set ga1[3] += 0x1
test::AssertEq#(ga1[3], 0x8765432187654322_s64)
-- gr1 s64
set gr1.i1 = 0x8765432187654321
test::AssertEq#(gr1.i1, 0x8765432187654321_s64)
set gr1.i1 += 0x1
test::AssertEq#(gr1.i1, 0x8765432187654322_s64)
-- gr1 u64
set gr1.i2 = 0x1234567812345678
test::AssertEq#(gr1.i2, 0x1234567812345678_u64)
set gr1.i2 -= 0x1
test::AssertEq#(gr1.i2, 0x1234567812345677_u64)
-- gr1 u64 via pointer
set get_addr()^.i2 = 0x1234567812345678
test::AssertEq#(get_addr()^.i2, 0x1234567812345678_u64)
set get_addr()^.i2 -= 0x1
test::AssertEq#(get_addr()^.i2, 0x1234567812345677_u64)
-- gar1 s64
set gar1[3].i1 = 0x8765432187654321
test::AssertEq#(gar1[3].i1, 0x8765432187654321_s64)
-- gr2 s64
set gr2.t3.i1 = 0x8765432187654321
test::AssertEq#(gr2.t3.i1, 0x8765432187654321_s64)
set gr2.t3.i1 += 0x1
test::AssertEq#(gr2.t3.i1, 0x8765432187654322_s64)
-- gr2 u64
set gr2.t3.i2 = 0x1234567812345678
test::AssertEq#(gr2.t3.i2, 0x1234567812345678_u64)
-- test end
test::Success#()
return 0
34 changes: 34 additions & 0 deletions FrontEnd/ConcreteSyntax/LangTest/defer_test.cw
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
-- defer
module main:

import test

global! gIndex uint = 0

global! gSequence = [10]u8{0}

fun store(c u8) void:
set gSequence[gIndex] = c
set gIndex += 1

fun foo() void:
defer:
shed store('h')
defer:
shed store('g')
shed store('a')
block _:
shed store('b')
defer:
shed store('e')
defer:
shed store('d')
shed store('c')
shed store('f')

@cdecl fun main(argc s32, argv ^^u8) s32:
shed foo()
test::AssertSliceEq#(slice(front(gSequence), gIndex), "abcdefgh")
-- test end
test::Success#()
return 0
72 changes: 72 additions & 0 deletions FrontEnd/ConcreteSyntax/LangTest/enum_test.cw
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
module main:

import test

@pub enum enum8 u8:
e1 7
e2 auto
e3 19
e4 auto

@pub enum enum16 u16:
e1 70
e2 auto
e3 190
e4 auto

@pub enum enum32 u32:
e1 700
e2 auto
e3 1900
e4 auto

-- GLOBAL
global! g1 = enum8:e1

global! g2 = enum16:e2

global! g3 = enum32:e3

@pub rec rec1:
-- this is a comment with \" with quotes \t
f1 s32
f2 s32
f3 s32
f4 bool
f5 enum8
f6 enum16
f7 enum32
f8 u64
f9 u64

global! gr1 rec1 = undef

@cdecl fun main(argc s32, argv ^^u8) s32:
-- LOCAL
let! v1 = enum8:e2
let! v2 = enum16:e3
let! v3 = enum32:e4
test::AssertEq#(g1, enum8:e1)
test::AssertEq#(g2, enum16:e2)
test::AssertEq#(g3, enum32:e3)
set g1 = v1
set g2 = v2
set g3 = v3
test::AssertEq#(g1, enum8:e2)
test::AssertEq#(g2, enum16:e3)
test::AssertEq#(g3, enum32:e4)
set v1 = enum8:e3
set v2 = enum16:e4
set v3 = enum32:e1
test::AssertEq#(v1, enum8:e3)
test::AssertEq#(v2, enum16:e4)
test::AssertEq#(v3, enum32:e1)
set gr1.f5 = enum8:e3
set gr1.f6 = enum16:e4
set gr1.f7 = enum32:e1
test::AssertEq#(gr1.f5, enum8:e3)
test::AssertEq#(gr1.f6, enum16:e4)
test::AssertEq#(gr1.f7, enum32:e1)
-- test end
test::Success#()
return 0
Loading

0 comments on commit d5bdd54

Please sign in to comment.