corth-lang / Corth

A self-hosted stack based language like Forth
MIT License
7 stars 1 forks source link

`promise` keyword #14

Open HuseyinSimsek7904 opened 9 months ago

HuseyinSimsek7904 commented 9 months ago

promise keyword will allow to declare procedures or variables without defining them. This is useful for creating functions that have cyclic dependencies.

Not defining a promised procedure or variable will cause a compiler error.

For example:

promise proc p2 int -- int end // this declares the procedure p2 with an integer argument and an integer output

proc p1
  int -- int
in let x in
  x is-even if
    x 2 / p1 return
  else
    x inc p2 return // this procedure was promised, therefore it can be called
  end
end end

proc p2 // defining the declared procedure
  int -- int
in let x in
  x 25 > if
    x return
  else
    x 3 * p1 return
  end
end end