c98828136de7dae5dc4a45f68edf8b9a5a3f3d6e — Martin Angers 7 months ago 22a3539 master
reflection on pointers/memory management
4 files changed, 30 insertions(+), 1 deletions(-)

A codegen/new.ssa
M syntax/declarations.snow
A syntax/modules.snow
A syntax/pointers.snow
A codegen/new.ssa => codegen/new.ssa +13 -0
@@ 0,0 1,13 @@
+ data $pfmt = { b "%p\n", b 0 }
+ data $vfmt = { b "%d\n", b 0 }
+ 
+ export function w $main() {
+ @start
+   %addr =l call $malloc(w 4)
+   call $printf(l $pfmt, l %addr)
+   storew 42, %addr
+   %val =w loadw %addr
+   call $printf(l $vfmt, w %val)
+   call $free(l %addr)
+   ret
+ }

M syntax/declarations.snow => syntax/declarations.snow +4 -1
@@ 14,6 14,8 @@ # protocols declare a "template" of fields, methods and extensions
  # TODO: can an enum match a protocol other than via methods?
  protocol Name {..}
+ # I-table:
+ # https://research.swtch.com/interfaces
  
  # There is no special way to declare an alias for built-in
  # types or any other type, just declare a struct with a single


@@ 56,9 58,10 @@ # closures/lambdas, longer form
  let func = (name type, name type) { ... }
  # short form for single-expression implicit return, the arguments
- # are provided on the "_" tuple.
+ # are provided on the "_" tuple. TODO: or $?
  let shortFunc = (type, type) => _.0 + _.1
  # if the arguments (number and types) can be inferred:
+ # TODO: not great, same syntax as to ignore labels
  let inferredFunc = (_) => _.0 + _.1
  
  ## Arrays and Slices

A syntax/modules.snow => syntax/modules.snow +0 -0

A syntax/pointers.snow => syntax/pointers.snow +13 -0
@@ 0,0 1,13 @@
+ ## Pointers
+ 
+ # `new` allocates on the heap and returns a pointer:
+ let pointerToInt = new int
+ # `&` returns a pointer to a thing
+ struct Person {
+   var age int
+   var name str
+ }
+ let p = Person{age: 33, name: "Tom"}
+ let pointer = &p
+ 
+ ## Memory management