/* iGASM example */ /* Evaluates expressions with binary operators (in RPN). */ UNIVERSES Operator = {"+","-","*","/","(",")"}; FUNCTIONS Arg1, Arg2 : Number; tape : List; st : Stack; INITIAL st := emptyStack; /* example: [10,3,4,"+","*"] ("10*(3+4)") */ tape := listAppend(listAppend(listAppend( listAppend(listAppend(emptyList, 10),3),4),"+"),"*"); FINAL tape = undef; RULES if Number(listHead(tape)) then st := stPush(st, listHead(tape)); tape := listTail(tape); elseif Operator(listHead(tape)) then if Arg1=undef then Arg1 := stTop(st); st := stPop(st); elseif Arg2=undef then Arg2 := stTop(st); st := stPop(st); else st := stPush(st, apply(listHead(tape), Arg2, Arg1)); tape := listTail(tape); Arg1 := undef; Arg2 := undef; endif else tape := undef; writeln := "Result is:\n" + toString(stTop(st)); endif