Up to Main Index                              Up to Journal for July, 2023

                      JOURNAL FOR SUNDAY 16TH JULY, 2023
______________________________________________________________________________

SUBJECT: Functioning functions
   DATE: Sun 16 Jul 19:44:48 BST 2023

Now Mere ICE v0.0.3 is out the door I can kick back and relax. Or I could get
on with implementing functions… I opted the latter :)

After two days coding I have an ugly implementation that just about works. I
can call functions, call functions recursively, call functions from functions,
pass arguments and return values:


    >cat fun.mr
    greet = call hello
    println "hello " greet
    println "hello ", call(hello "you"), "!"
    println "add: " call add 3 5
    println "stars: " call stars ""
    println "a calls b: " call funcA
    exit

    // Return simple string
    hello:
      if !exists @ 0; return "who are you?"
      return @[0]
    endfunc

    // Return calculate value
    add: func
      return @[0] + @[1]
    endfunc

    // Recursive function to build a string
    stars: func
      if len(@[0] += "*") == 3; return @[0]
      return call stars @[0]
    endfunc

    //Simple function called by another function
    funcB: func
      return "B"
    endfunc

    // Function calling a function
    funcA: func
      return "A" + call funcB
    endfunc

    >mere fun.mr
    hello who are you?
    hello you!
    add: 8
    stars: ***
    a calls b: AB
    >


Functions are very quirky at the moment, but this is more of an experimental
draft version. For example, parameters are passed in a very Perlish way using
an integer map [int] called ‘@’. I’m working on named parameters. Functions
are currently defined using labels, very odd but works for a quick initial
“get it working”. Functions can call other functions, but they currently need
to be defined before they are called, which is a bit of a pain. The example
above does not show it but variables inside functions are scoped to the
function.

Still, after only a few days coding, I’m quite happy with the initial results.

--
Diddymus


  Up to Main Index                              Up to Journal for July, 2023