From 1ad6779aa4ff13ea00bcfe6320c4d9af1326a982 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Thu, 3 Aug 2023 22:34:43 +0900 Subject: [PATCH] a bit of progress in writing the evaluator function --- interp/eval.go | 61 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 50 insertions(+), 11 deletions(-) diff --git a/interp/eval.go b/interp/eval.go index ad14c3b..50b22c8 100644 --- a/interp/eval.go +++ b/interp/eval.go @@ -31,6 +31,7 @@ func (p *process_t) push_cnode_value(val *Cnode_t) error { p.vstack[p.vsp] = unsafe.Pointer(uintptr(unsafe.Pointer(val)) | 1) p.vsp++ p.ctx.count++ + fmt.Printf("push_cnode_value = ctx.count => %d\n", p.ctx.count) return nil } @@ -43,7 +44,7 @@ func (p *process_t) push_string_value(val string) error { p.vstack[p.vsp] = unsafe.Pointer(&val) p.vsp++ p.ctx.count++ - + fmt.Printf("push_string_value = ctx.count => %d\n", p.ctx.count) return nil } @@ -58,6 +59,7 @@ func (p *process_t) merge_top_values() error { p.vstack[p.vsp] = nil p.vstack[p.vsp-1] = unsafe.Pointer(&new_val) p.ctx.count-- + fmt.Printf("merge_top_values = ctx.count => %d\n", p.ctx.count) return nil } @@ -124,7 +126,7 @@ func (p *process_t) pop_context(clear_vstack bool) (*Cnode_t, *Cnode_t) { node = p.ctx.parent_node container = p.ctx.container_node - if clear_vstack { + if clear_vstack { // TODO: use the conttext type instead... may be able to use container_node.code??? // clean up the unused part of the stack for i = 1; i < p.ctx.count; i++ { p.vstack[p.vsp-p.ctx.count+i] = nil @@ -135,9 +137,9 @@ func (p *process_t) pop_context(clear_vstack bool) (*Cnode_t, *Cnode_t) { } p.ctx = p.ctx.parent_ctx - if p.ctx != nil { - p.ctx.count++ // let the return value be the argument to the caller - } +// if p.ctx != nil { +// p.ctx.count++ // let the return value be the argument to the caller +// } return node, container } @@ -205,7 +207,7 @@ func (interp *Interp) eval_stmt_nodes(container_node *Cnode_t) (*string, error) panic("internal error - not statement node") } - //fmt.Printf("PUSHING CONTEXT.....\n") + fmt.Printf("PUSHING CONTEXT.....\n") p.push_context(stmt_node, upper_node) p.push_string_value("") // placeholder for return value @@ -225,6 +227,7 @@ func (interp *Interp) eval_stmt_nodes(container_node *Cnode_t) (*string, error) fmt.Printf("\n--\n") } goto start_over_0 + } else { // no statements inside []. treat it like an empty string p.push_string_value("") @@ -232,7 +235,7 @@ func (interp *Interp) eval_stmt_nodes(container_node *Cnode_t) (*string, error) case CNODE_DQUOTE: if inner_node.child != nil { - //fmt.Printf("PUSHING CONTEXT.....\n") + fmt.Printf("PUSHING CONTEXT.....\n") p.push_context(stmt_node, inner_node) //p.push_string_value("") // no placeholder for return value is needed inner_node = inner_node.child @@ -284,22 +287,58 @@ func (interp *Interp) eval_stmt_nodes(container_node *Cnode_t) (*string, error) is_stmt = false } - //fmt.Printf("POPPING CONTEXT.....is_stmt[%v]\n", is_stmt) + fmt.Printf("POPPING CONTEXT.....is_stmt[%v]\n", is_stmt) 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) } - inner_node = upper_node.next // as if it hit the bottom of the innner for loop - goto resume + + 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 + } } //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") + } + fmt.Printf("POPPING CONTEXT.....false\n") + //stmt_node, upper_node = p.pop_context(false) + 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 + } + + fmt.Printf("POPVAL...\n") + v = (*string)(p.pop_value()) // get the return value of the statement. + if debug { + interp.dump_vstack(&p) + } + break + } + + fmt.Printf("POPVAL...\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 debug {