Conditionals and Functions, Oh My!

Ok, with a bit of effort (and excessive quoting) I have conditionals working, and LISP functions. Until I make the parser programmable, any expressions that need delayed evaluation need to be enclosed in strings. The relevant functions (if, defun) simply use the same internal logic as eval does to evaluate the selected expressions. Currently, functions cannot take arguments, though they may return results. Recursion also appears to work!

Functions are actually precompiled, so they aren’t too bad, but the arguments to if are compiled each time, which is slow.

It was remarkably easy to get this much to work (ugly as it is at the moment). I think I’m getting lucky so far, I have a feeling that one of the milestones I aim for soon will be much tougher, and possibly force me to rethink a lot of stuff.

Some potential candidates for that are
* local variables and function arguments
* the aforementioned parser modifications to remove the need for quoting certain expressions
* introducing reference semantics (note 1)
* implementing loops and conditionals as operations in the bytecode interpreter, rather than functions (note 2)

> 1
Currently everything uses copy semantics. When I get recursive functions with arguments, trying to run complex data structure algorithms will probably be very expensive and unbearably slow for no reason. Of course, references may require some kind of rudimentary garbage collection, which is more than capable of causing its own headaches.
> 2
I don’t know if I’ll bother with this one unless it proves necessary. It probably will be though to avoid having the expressions passed to if being constantly re-evaluated.

While I haven’t bothered implementing a while loop yet, I think it should be fairly easy to rig something up which would act a bit like if and defun at the moment.

Some sample scripts:

-- easy: this would work even if the arguments were fully evaluated
--  if() could simply conditionally return the relevant argument
-- this could have been implemented under my old system
(print (if "(< 4 5)" "'foo" "'bar"))
(print (if "(< 5 4)" "'foo" "'bar"))

-- harder: only one print statement should execute
-- not possible under old system (all function arguments were fully evaluated before call)
(if "(< 4 5)" "(print 1)" "(print 2)" )
(if "(> 4 5)" "(print 3)" "(print 4)" )

-- simple function
(defun 'foo '() "(print \"hello world\")" )
(foo)

-- return values
(defun 'sample '() "42" )

(print (sample))

-- recursion!
(global 'a 42)

(defun 'dec '() "(global 'a (- a 1))" )


(defun 'recurse '() "(if \"(> a 0)\" \"(dec) (recurse)\" \"(print a)\" )" )

(recurse)

Output:

foo
bar
1
4
hello world
42
0
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: