|Newbie questions about Pliant
Parsing and evaluating an expression
I have a string, say "4 + 3", and I would like to evaluate
it, so that I get an Arrow back that points to a
new uInt 7.
What steps do I follow?
|Message posted by maybe Gord Eagle on 2003/01/02 00:22:17
|I ask this because I'm almost done implementing a C preprocessor Stream.
Given another stream specification (like cpp:file:/src/grub/main.c), it
returns data as if it were preprocessed by cpp.
This is a first step towards implementing a full C syntax module for Pliant.
But first, I need to figure out how to evaluate single expressions (for
the #if statements in cpp).
|Message posted by maybe Gord Eagle on 2003/01/02 00:22:55
|BTW, the expressions are guaranteed to be constant.
|Message posted by maybe on 2003/01/02 00:39:06
|> I ask this because I'm almost done implementing a C preprocessor Stream.
> But first, I need to figure out how to evaluate single expressions (for
> the #if statements in cpp).
That really easy.
For any already sucessfully compiled expression 'e'
is the type of the result and
e constant e:result:type
is the address of the constant result, or 'null' if the result is not constant.
Here is a tiny demonstration script:
meta what_s_this e
e:0 compile ?
console "result type is: " e:0:result:type:name eol
var Address cst := e:0 constant e:0:result:type
console "result value is " (to_string cst e:0:result:type "") eol
|Message posted by maybe Gord Eagle on 2003/01/02 03:53:00
|That's not the part I was having difficulty with.
Given a string "3 + 4", how do I get an Expression using my own
'pliant parser sections'?
|Message posted by maybe Hubert Tonneau on 2003/01/02 11:04:59
|I hope this is what you expect.
Through calling 'evaluate' with a different module containing your own
parser, it should be possible to evaluate the expression within your own
syntax, also the expression
"'pick the result' ("+text+")"
might need to be modified.
gvar Arrow the_result
meta 'pick the result' e
e:0 compile ?
the_result := e:0 constant e:0:result:type
export 'pick the result'
function evaluate text env -> result
arg Str text env ; arg Arrow result
result := null
var Link:Module e :> pliant_load_module env the_module:"/pliant/language/basic/safe.pli" 0 (null map Module)
if not exists:e
var Link:Module m :> new Module
m name := "sample expression"
m include e
m include this_module
var List l
l append addressof:(new Str "'pick the result' ("+text+")")
var Arrow memo := the_result
compile_text l m
result := the_result ; the_result := memo
var Arrow r := evaluate "3 + 4" "/pliant/language/basic/safe.pli"
console "type of the result is: " entry_type:r:name eol
console "result value is " (to_string r entry_type:r "") eol
|Message posted by maybe Hubert Tonneau on 2003/01/02 11:18:20
|Also as an even better implementation, your environment might contain a
replacement of the standard 'parser_filter_execute' function that would do
the work done by 'pick the result' so that there is no need to encapsulate
the expression anymore.
|Message posted by maybe Gord Eagle on 2003/01/05 20:45:48
|Thanks, your advice worked fine.