pcl/interp/eval.go

536 lines
12 KiB
Go
Raw Permalink Normal View History

2023-07-21 09:32:51 +00:00
package interp
import (
"fmt"
2023-08-13 16:08:37 +00:00
"unsafe"
2023-07-21 09:32:51 +00:00
)
var debug bool = false
2023-07-21 09:32:51 +00:00
var err_num_args *error_t = &error_t{msg: "wrong number of arguments"}
2023-08-13 16:08:37 +00:00
var empty_strval = Value_t{Kind: VALUE_STR, V: unsafe.Pointer(new(string))}
2023-07-21 09:32:51 +00:00
/*
2023-08-03 13:34:42 +00:00
value stack (p.vstack)
2023-07-21 09:32:51 +00:00
2023-08-03 13:34:42 +00:00
<--- SP (p.vsp)
2023-07-21 09:32:51 +00:00
ARG1
ARG0
NAME
RET
evaluation stack (p.ctx)
*/
2023-08-08 15:57:19 +00:00
func (p *process_t) push_call_frame() {
var cf *call_frame_t
cf = &call_frame_t{}
if p.cframe == nil {
// let it point to the global frame located in the interp struct
cf.parent = p.interp.cframe
} else {
cf.parent = p.cframe
}
2023-08-13 16:08:37 +00:00
cf.vars = make(map[string]Value_t)
2023-08-08 15:57:19 +00:00
p.cframe = cf
}
func (p *process_t) pop_call_frame() {
if p.cframe == p.interp.cframe {
p.cframe = nil
} else {
p.cframe = p.cframe.parent
}
}
2023-07-21 09:32:51 +00:00
func (p *process_t) push_cnode_value(val *Cnode_t) error {
2023-08-03 13:34:42 +00:00
if p.vsp >= cap(p.vstack) {
2023-07-21 09:32:51 +00:00
return fmt.Errorf("stack full")
}
2023-08-13 16:08:37 +00:00
p.vstack[p.vsp] = Value_t{Kind: VALUE_CNODE, V: unsafe.Pointer(val)}
2023-08-03 13:34:42 +00:00
p.vsp++
2023-07-21 09:32:51 +00:00
p.ctx.count++
//fmt.Printf("push_cnode_value = ctx.count => %d\n", p.ctx.count)
2023-07-21 09:32:51 +00:00
return nil
}
func (p *process_t) push_string_value(val string) error {
2023-08-03 13:34:42 +00:00
if p.vsp >= cap(p.vstack) {
2023-07-21 09:32:51 +00:00
return fmt.Errorf("stack full")
}
2023-08-13 16:08:37 +00:00
p.vstack[p.vsp] = Value_t{Kind: VALUE_STR, V: unsafe.Pointer(&val)}
2023-08-03 13:34:42 +00:00
p.vsp++
2023-07-21 09:32:51 +00:00
p.ctx.count++
//fmt.Printf("push_string_value = ctx.count => %d\n", p.ctx.count)
2023-07-21 09:32:51 +00:00
return nil
}
func (p *process_t) merge_top_values() error {
var new_val string
2023-08-13 16:08:37 +00:00
var v1, v2 Value_t
2023-07-21 09:32:51 +00:00
2023-08-03 13:34:42 +00:00
if p.vsp < 2 {
2023-07-21 09:32:51 +00:00
return fmt.Errorf("stack corrupt")
}
2023-08-13 16:08:37 +00:00
v1 = p.vstack[p.vsp-2]
v2 = p.vstack[p.vsp-1]
2023-08-08 15:57:19 +00:00
2023-08-13 16:08:37 +00:00
if v1.Kind == VALUE_STR {
new_val += *(*string)(v1.V)
2023-08-08 15:57:19 +00:00
}
2023-08-13 16:08:37 +00:00
// TODO: correct this to get the original text inside{}
// or must panic here by making {} unmergable in the feeder side
if v2.Kind == VALUE_STR {
new_val += *(*string)(v2.V)
2023-08-08 15:57:19 +00:00
}
2023-08-13 16:08:37 +00:00
2023-08-03 13:34:42 +00:00
p.vsp--
2023-08-13 16:08:37 +00:00
p.vstack[p.vsp].V = unsafe.Pointer(nil)
p.vstack[p.vsp-1] = Value_t{Kind: VALUE_STR, V: unsafe.Pointer(&new_val)}
2023-07-21 09:32:51 +00:00
p.ctx.count--
if debug {
fmt.Printf("merge_top_values = ctx.count => %d\n", p.ctx.count)
}
2023-07-21 09:32:51 +00:00
return nil
}
func (p *process_t) pop_value() Value_t {
var v Value_t
2023-08-03 13:34:42 +00:00
p.vsp--
v = p.vstack[p.vsp]
2023-08-13 16:08:37 +00:00
p.vstack[p.vsp].V = unsafe.Pointer(nil)
2023-07-21 09:32:51 +00:00
return v
}
func (p *process_t) call() error {
var (
proc func(*process_t) error
callee *string
)
callee = p.GetCalleeName()
if debug {
fmt.Printf("calling..... [%s]\n", *callee)
}
2023-07-21 09:32:51 +00:00
// TODO: use a map
switch *callee {
2023-08-08 15:57:19 +00:00
case "proc":
proc = proc_proc
case "set":
proc = proc_set
2023-07-21 09:32:51 +00:00
case "if":
proc = proc_if
case "puts":
proc = proc_puts
2023-08-03 13:34:42 +00:00
case "true":
proc = proc_true
case "false":
proc = proc_false
case "null":
proc = proc_null
2023-07-21 09:32:51 +00:00
default:
proc = proc_unknown
}
return proc(p)
}
func (p *process_t) GetCalleeName() *string {
2023-08-13 16:08:37 +00:00
return (*string)(p.vstack[p.vsp-p.ctx.count+1].V)
2023-07-21 09:32:51 +00:00
}
func (p *process_t) GetArg(idx int) Value_t {
2023-08-03 13:34:42 +00:00
return (p.vstack[p.vsp-p.ctx.count+2+idx])
2023-07-21 09:32:51 +00:00
}
func (p *process_t) GetNumArgs() int {
return p.ctx.count - 2
}
func (p *process_t) ReturnString(val string) {
2023-08-13 16:08:37 +00:00
p.vstack[p.vsp-p.ctx.count] = Value_t{Kind: VALUE_STR, V: unsafe.Pointer(&val)}
}
func (p *process_t) Return(val Value_t) {
p.vstack[p.vsp-p.ctx.count] = val
2023-07-21 09:32:51 +00:00
}
2023-08-03 13:34:42 +00:00
func (p *process_t) push_context(node *Cnode_t, container_node *Cnode_t) {
if debug {
fmt.Printf("PUSHING CONTEXT.....\n")
}
2023-08-03 13:34:42 +00:00
p.ctx = &context_t{count: 0, parent_ctx: p.ctx, parent_node: node, container_node: container_node}
2023-07-21 09:32:51 +00:00
}
2023-08-03 13:34:42 +00:00
func (p *process_t) pop_context(clear_vstack bool) (*Cnode_t, *Cnode_t) {
2023-08-03 13:34:42 +00:00
var (
i int
node *Cnode_t
container *Cnode_t
)
2023-07-21 09:32:51 +00:00
if debug {
fmt.Printf("POPPING CONTEXT.....is_stmt/clear_vstack[%v]\n", clear_vstack)
}
2023-07-21 09:32:51 +00:00
node = p.ctx.parent_node
2023-08-03 13:34:42 +00:00
container = p.ctx.container_node
2023-07-21 09:32:51 +00:00
if clear_vstack { // TODO: use the conttext type instead... may be able to use container_node.code???
2023-08-03 13:34:42 +00:00
// clean up the unused part of the stack
for i = 1; i < p.ctx.count; i++ {
2023-08-13 16:08:37 +00:00
p.vstack[p.vsp-p.ctx.count+i].V = unsafe.Pointer(nil)
2023-08-03 13:34:42 +00:00
}
2023-07-21 09:32:51 +00:00
2023-08-03 13:34:42 +00:00
// pop off the cleaned arguments
p.vsp -= p.ctx.count - 1 // keep the return value in the stack
}
2023-07-21 09:32:51 +00:00
p.ctx = p.ctx.parent_ctx
// if p.ctx != nil {
// p.ctx.count++ // let the return value be the argument to the caller
// }
2023-07-21 09:32:51 +00:00
2023-08-03 13:34:42 +00:00
return node, container
}
2023-07-21 09:32:51 +00:00
2023-08-03 13:34:42 +00:00
func (interp *Interp) dump_vstack(p *process_t) {
fmt.Printf("p.VSP => %d\n", p.vsp)
for i := 0; i < p.vsp; i++ {
/*
x := uintptr(p.vstack[i])
if x&1 == 0 {
// string value
fmt.Printf(" %d => [%s]\n", i, *(*string)(p.vstack[i]))
} else {
// cnode value
fmt.Printf(" %d => cnode %p", i, p.vstack[i]) // TODO: strip 1 off
}*/
2023-08-13 16:08:37 +00:00
switch p.vstack[i].Kind {
case VALUE_STR:
fmt.Printf(" %d => [%s]\n", i, *(*string)(p.vstack[i].V))
case VALUE_CNODE:
fmt.Printf(" %d => ", i)
2023-08-13 16:08:37 +00:00
interp.dump_cnodes((*Cnode_t)(p.vstack[i].V), false)
fmt.Printf("\n")
default:
panic("internal error - unrecognized value")
2023-08-03 13:34:42 +00:00
}
}
}
/*
puts "hello" world
[STMT]
[TEXT|puts] [DQUOTE] [TEXT|world]
[TEXT|hello]
[puts 1 2; puts 1] 999
[STMT]
[BRACKET] [TEXT|999]
[STMT]
[TEXT|puts] [TEXT|1] [TEXT|2]
[STMT]
[TEXT|puts] [TEXT|1]
"pu[null 1]ts" 10 20
[STMT]
[DQUOTE] [TEXT|10] [TEXT|20]
[TEXT|pu] [BRACKET] [JOIN] [TEXT|ts] [JOIN]
[STMT]
[TEXT|null] [TEXT|1]
*/
2023-08-08 15:57:19 +00:00
func (interp *Interp) eval_stmt_nodes(p *process_t, container_node *Cnode_t) (Value_t, error) {
2023-07-21 09:32:51 +00:00
var (
v Value_t
2023-08-03 13:34:42 +00:00
stmt_node *Cnode_t
upper_node *Cnode_t
2023-08-03 13:34:42 +00:00
inner_node *Cnode_t
is_stmt bool
2023-08-03 13:34:42 +00:00
err error
org_vsp int
2023-07-21 09:32:51 +00:00
)
2023-08-13 16:08:37 +00:00
v = empty_strval
2023-08-03 13:34:42 +00:00
upper_node = container_node
stmt_node = upper_node.child // the first statement
2023-08-03 13:34:42 +00:00
org_vsp = p.vsp
2023-08-03 13:34:42 +00:00
fmt.Printf("START p.sp = %d\n", p.vsp)
if stmt_node == nil {
goto done
}
for {
start_over_0:
2023-08-03 13:34:42 +00:00
if stmt_node.code != CNODE_STMT {
panic("internal error - not statement node")
}
p.push_context(stmt_node, upper_node)
2023-07-21 09:32:51 +00:00
p.push_string_value("") // placeholder for return value
2023-08-03 13:34:42 +00:00
inner_node = stmt_node.child
resume:
for inner_node != nil {
2023-08-03 13:34:42 +00:00
//fmt.Printf("handling %d\n", inner_node.code)
2023-08-03 13:34:42 +00:00
switch inner_node.code {
2023-07-21 09:32:51 +00:00
case CNODE_BRACKET:
2023-08-03 13:34:42 +00:00
if inner_node.child != nil {
upper_node = inner_node
stmt_node = upper_node.child
if debug {
fmt.Printf("going to start over\n")
interp.dump_cnodes(stmt_node, true)
fmt.Printf("\n--\n")
}
goto start_over_0
2023-08-03 13:34:42 +00:00
} else {
// no statements inside []. treat it like an empty string
p.push_string_value("")
}
2023-07-21 09:32:51 +00:00
2023-08-03 13:34:42 +00:00
case CNODE_DQUOTE:
if inner_node.child != nil {
p.push_context(stmt_node, inner_node)
//p.push_string_value("") // no placeholder for return value is needed
inner_node = inner_node.child
if debug {
fmt.Printf("going to start over\n")
interp.dump_cnodes(stmt_node, true)
fmt.Printf("\n--\n")
}
2023-08-03 13:34:42 +00:00
goto resume
} else {
// no statements inside []. treat it like an empty string
p.push_string_value("")
}
2023-08-03 13:34:42 +00:00
case CNODE_BRACE:
p.push_cnode_value(inner_node)
2023-07-21 09:32:51 +00:00
case CNODE_TEXT:
2023-08-03 13:34:42 +00:00
//fmt.Printf("XXXXXXXXXXXXXXXXXXXx[%s]\n", string(inner_node.token))
2023-08-03 13:34:42 +00:00
err = p.push_string_value(string(inner_node.token))
2023-07-21 09:32:51 +00:00
if err != nil {
goto oops
}
2023-08-03 13:34:42 +00:00
// TODO: many more types...
2023-08-03 13:34:42 +00:00
case CNODE_JOIN:
p.merge_top_values()
case CNODE_INIT:
panic("internal error - INIT node must not appear inside a statement")
2023-07-21 09:32:51 +00:00
}
2023-08-03 13:34:42 +00:00
inner_node = inner_node.next
2023-07-21 09:32:51 +00:00
}
if debug {
2023-08-08 15:57:19 +00:00
interp.dump_vstack(p)
}
//fmt.Printf("p.ctx.parent_node.code %d p.ctx.container_node.code %d CNODE_STMT %d CNODE_DQUOTE %d CNODE_BRACKET %d\n", p.ctx.parent_node.code, p.ctx.container_node.code, CNODE_STMT, CNODE_DQUOTE, CNODE_BRACKET)
if p.ctx.container_node.code == CNODE_INIT || p.ctx.container_node.code == CNODE_BRACKET || p.ctx.container_node.code == CNODE_BRACE {
2023-08-03 13:34:42 +00:00
err = p.call()
if err != nil {
goto oops
}
2023-08-03 13:34:42 +00:00
is_stmt = true
2023-08-03 13:34:42 +00:00
} else {
is_stmt = false
2023-07-21 09:32:51 +00:00
}
stmt_node, upper_node = p.pop_context(is_stmt)
if upper_node != container_node {
if debug {
fmt.Printf("resuming... %d upper_node.next %p\n", p.vsp, upper_node.next)
}
if upper_node.code != CNODE_BRACKET {
inner_node = upper_node.next // as if it hit the bottom of the innner for loop
p.ctx.count++ // use return value on the stack as an argument
goto resume
}
2023-08-03 13:34:42 +00:00
}
2023-07-21 09:32:51 +00:00
//fmt.Printf("POPPING VALUE...\n")
/*v = (*string)(p.pop_value()) // get the return value of the statement.
if debug {
interp.dump_vstack(&p)
}*/
stmt_node = stmt_node.next
if stmt_node == nil {
// go doesn't allow jumping into a block.
// let's the put the code here
if upper_node != container_node {
// the upper node is not the top containing node passed to this function.
// the contenxt stack must not be empty in this case.
if upper_node.code != CNODE_BRACKET {
panic("internal error - invalid cnode type in the context statck")
}
inner_node = upper_node.next
//fmt.Printf(">>>>>>>>>>>>>>>>>> vsp %d ctx.count %d\n", p.vsp, p.ctx.count)
p.ctx.count++ // use the result value as an argument
goto resume
}
v = p.pop_value() // get the return value of the statement.
if debug {
2023-08-08 15:57:19 +00:00
interp.dump_vstack(p)
}
break
}
v = p.pop_value() // get the return value of the statement.
if debug {
2023-08-08 15:57:19 +00:00
interp.dump_vstack(p)
}
2023-07-21 09:32:51 +00:00
}
done:
if debug {
2023-08-08 15:57:19 +00:00
interp.dump_vstack(p)
fmt.Printf("END p.sp = %d\n", p.vsp)
}
if p.vsp != org_vsp {
panic("internal error - stack not clean")
}
2023-07-21 09:32:51 +00:00
return v, nil
oops:
2023-08-13 16:08:37 +00:00
return empty_strval, err
2023-07-21 09:32:51 +00:00
}
func (interp *Interp) eval_arg(p *process_t, pos int) (Value_t, error) {
2023-08-13 16:08:37 +00:00
var v Value_t
2023-07-21 09:32:51 +00:00
2023-08-13 16:08:37 +00:00
v = p.GetArg(pos)
switch v.Kind {
case VALUE_STR:
return v, nil
case VALUE_CNODE:
return interp.eval_stmt_nodes(p, (*Cnode_t)(v.V))
default:
panic("internal error - argument type unrecognized")
2023-07-21 09:32:51 +00:00
}
}
func (interp *Interp) eval_arg_literally(p *process_t, pos int) (Value_t, error) {
/*
var (
ptr uintptr
//cnode *Cnode_t
)
ptr = uintptr(p.GetArg(pos))
if ptr&1 == 1 { // cnode
ptr &= ^uintptr(1)
//cnode = (*Cnode_t)(unsafe.Pointer(ptr))
//cnode.child i hate this portion....
return nil, fmt.Errorf("not supported - unable to evaluate {} literally")
} else {
return (*string)(unsafe.Pointer(ptr)), nil
}*/
2023-07-21 09:32:51 +00:00
2023-08-13 16:08:37 +00:00
var v Value_t
v = p.GetArg(pos)
switch v.Kind {
case VALUE_STR:
return v, nil
case VALUE_CNODE:
// TODO: can support this? by storing the original text?
2023-08-13 16:08:37 +00:00
return empty_strval, fmt.Errorf("not supported - unable to evaluate {} literally")
default:
panic("internal error - argument type unrecognized")
2023-07-21 09:32:51 +00:00
}
}
2023-08-08 15:57:19 +00:00
func (interp *Interp) set_var(p *process_t, name Value_t, val Value_t) error {
2023-08-13 16:08:37 +00:00
if name.Kind != VALUE_STR {
return fmt.Errorf("invalid variable name")
}
2023-08-08 15:57:19 +00:00
2023-08-13 16:08:37 +00:00
// TODO: error check?
p.cframe.vars[*(*string)(name.V)] = val
2023-08-08 15:57:19 +00:00
return nil
}
2023-08-13 16:08:37 +00:00
func (interp *Interp) get_var(p *process_t, name Value_t) (Value_t, error) {
var (
key *string
val Value_t
f *call_frame_t
ok bool
)
if name.Kind != VALUE_STR {
return empty_strval, fmt.Errorf("invalid variable name")
}
key = (*string)(name.V)
for f = p.cframe; f != nil; f = f.parent {
val, ok = p.cframe.vars[*key]
if ok {
return val, nil
}
}
return empty_strval, fmt.Errorf("%s not found", *key)
}
func (interp *Interp) EvalText(text []rune) (Value_t, error) {
2023-07-21 09:32:51 +00:00
var (
v Value_t
2023-07-21 09:32:51 +00:00
node *Cnode_t
err error
)
interp.BeginFeed() // this resets the feed stack to the initial state
err = interp.FeedRunes(text)
if err != nil {
goto oops
}
node, err = interp.EndFeed()
if err != nil {
goto oops
}
//fmt.Printf("--------------------\n")
//interp.dump_cnodes(node, true)
//fmt.Printf("--------------------\n")
v, err = interp.Execute(node)
if err != nil {
goto oops
}
return v, nil
oops:
2023-08-13 16:08:37 +00:00
return empty_strval, err
2023-07-21 09:32:51 +00:00
}