From acdae7bab43c4dca4ff03be1467b2fc86e7bbe1a Mon Sep 17 00:00:00 2001 From: dos-reis Date: Sat, 29 May 2010 15:07:35 +0000 Subject: Add more utility functions --- src/interp/g-util.boot | 56 +++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 51 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/interp/g-util.boot b/src/interp/g-util.boot index 18af0463..7a93d95a 100644 --- a/src/interp/g-util.boot +++ b/src/interp/g-util.boot @@ -46,6 +46,51 @@ module g_-util where expandToVMForm: %Thing -> %Thing +--% VM forms + +++ Make the assumption named `prop' for all symbols +++ on the lis `syms'. +assumeProperty(syms,prop) == + for s in syms repeat + property(s,prop) := true + +assumeProperty('(%and %or),'%nary) + +++ We are about to construct a middle end expression +++ with operator `op, and aguments `form'. Try to +++ simplify the structure of the expression. +flattenVMForm(form,op) == main where + main() == + atom form => form + EQ(form.op,op) => [op,:flatten(form.args,op,nil)] + [flattenVMForm(form.op,op),:flattenVMForm(form.args,op)] + flatten(forms,op,acc) == + forms = nil => accu + x := flattenVMForm(first forms,op) + cons? x and EQ(x.op,op) => flatten(rest forms,op,[:accu,:x.args]) + flatten(rest forms,op,[:accu,x]) + +++ Build a midde end expression with given operator and arguments. +mkVMForm(op,args) == + if op has %nary then + args := flattenVMForm(args,op) + op = '%or => + args := REMOVE('%false,args) + args = nil => '%false + args is [arg] => arg + [op,:args] + op = '%and => + args := REMOVE('%true,args) + args = nil => '%true + args is [arg] => arg + [op,:args] + op = '%not => + [arg] := args + arg = '%false => '%true + arg = '%true => '%false + arg is ['%not,arg'] => arg' + ['%not,:args] + --% --% Opcode expansion to VM codes. --% @@ -568,13 +613,14 @@ getTypeOfSyntax t == bool: %Thing -> %Boolean bool x == null null x - + +++ Return true is the form `x' is a predicate known to always +++ evaluate to true. TruthP x == - --True if x is a predicate that's always true - x is nil => nil - x=true => true + x = nil or x = '%false => false + x = true or x = '%true => true x is ['QUOTE,:.] => true - nil + false --% Record and Union utils. -- cgit v1.2.3