rxi / fe

A tiny, embeddable language implemented in ANSI C
MIT License
1.3k stars 81 forks source link

Implementing OOP (Structs or objects) for fe #13

Closed ghost closed 3 years ago

ghost commented 3 years ago

Hi @rxi ! I'm so interested in fe and liked it so much, However... I didn't found any example/way about implementing Object-Oriented stuff for fe!

I tried to implement struct via this way:

(do
  ; Value from list by index
  (= nth (fn (n lst)
    (while (< 0 n)
      (= n (- n 1))
      (= lst (cdr lst)))
    (if (is n 0) (car lst))
  ))

  ; Trick to make Structs ;)
  ; Create list storing cons(s), Like structs in C
  (= obj (list
    (cons "A" 1)
    (cons "B" 2)
    (cons "C" 3)
    (cons "D" 4)
  ))

  ; Get struct prop (Remember that list works like array)
  (print (car (nth 3 obj)))
  (print (cdr (nth 3 obj)))

  (if (is (car (nth 3 obj)) "D")
    (print "STRUCTS IMPLEMENTATION WORKED!")
  )
)

But i think this gets much difficult to get property of struct, Is there some way in C/fe to implement OOP stuff?

Thanks!

ghost commented 3 years ago

I did ported lib.lsp of aria to work with fe, It's working greatly thanks to your hard work on both languages!