implementing the evaluator
This commit is contained in:
parent
5d66a2e6d7
commit
c1f7c4b963
@ -8,9 +8,9 @@ import (
|
||||
var err_num_args *error_t = &error_t{msg: "wrong number of arguments"}
|
||||
|
||||
/*
|
||||
value stack (p.stack)
|
||||
value stack (p.vstack)
|
||||
|
||||
<--- SP
|
||||
<--- SP (p.vsp)
|
||||
ARG1
|
||||
ARG0
|
||||
NAME
|
||||
@ -20,24 +20,26 @@ var err_num_args *error_t = &error_t{msg: "wrong number of arguments"}
|
||||
*/
|
||||
|
||||
func (p *process_t) push_cnode_value(val *Cnode_t) error {
|
||||
if p.sp >= cap(p.stack) {
|
||||
if p.vsp >= cap(p.vstack) {
|
||||
return fmt.Errorf("stack full")
|
||||
}
|
||||
|
||||
p.stack[p.sp] = unsafe.Pointer(uintptr(unsafe.Pointer(val)) | 1)
|
||||
p.sp++
|
||||
// TODO: using the last bit won't be compatible with go's GC.
|
||||
// CHANGE to use inteface{} or devise a different scheme...
|
||||
p.vstack[p.vsp] = unsafe.Pointer(uintptr(unsafe.Pointer(val)) | 1)
|
||||
p.vsp++
|
||||
p.ctx.count++
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *process_t) push_string_value(val string) error {
|
||||
if p.sp >= cap(p.stack) {
|
||||
if p.vsp >= cap(p.vstack) {
|
||||
return fmt.Errorf("stack full")
|
||||
}
|
||||
|
||||
p.stack[p.sp] = unsafe.Pointer(&val)
|
||||
p.sp++
|
||||
p.vstack[p.vsp] = unsafe.Pointer(&val)
|
||||
p.vsp++
|
||||
p.ctx.count++
|
||||
|
||||
return nil
|
||||
@ -46,22 +48,22 @@ func (p *process_t) push_string_value(val string) error {
|
||||
func (p *process_t) merge_top_values() error {
|
||||
var new_val string
|
||||
|
||||
if p.sp < 2 {
|
||||
if p.vsp < 2 {
|
||||
return fmt.Errorf("stack corrupt")
|
||||
}
|
||||
new_val = *(*string)(p.stack[p.sp-2]) + *(*string)(p.stack[p.sp-1])
|
||||
p.sp--
|
||||
p.stack[p.sp] = nil
|
||||
p.stack[p.sp-1] = unsafe.Pointer(&new_val)
|
||||
new_val = *(*string)(p.vstack[p.vsp-2]) + *(*string)(p.vstack[p.vsp-1])
|
||||
p.vsp--
|
||||
p.vstack[p.vsp] = nil
|
||||
p.vstack[p.vsp-1] = unsafe.Pointer(&new_val)
|
||||
p.ctx.count--
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *process_t) pop_value() unsafe.Pointer {
|
||||
var v unsafe.Pointer
|
||||
p.sp--
|
||||
v = p.stack[p.sp]
|
||||
p.stack[p.sp] = nil
|
||||
p.vsp--
|
||||
v = p.vstack[p.vsp]
|
||||
p.vstack[p.vsp] = nil
|
||||
return v
|
||||
}
|
||||
|
||||
@ -85,11 +87,11 @@ func (p *process_t) call() error {
|
||||
}
|
||||
|
||||
func (p *process_t) GetCalleeName() *string {
|
||||
return (*string)(p.stack[p.sp-p.ctx.count+1])
|
||||
return (*string)(p.vstack[p.vsp-p.ctx.count+1])
|
||||
}
|
||||
|
||||
func (p *process_t) GetArg(idx int) unsafe.Pointer {
|
||||
return (p.stack[p.sp-p.ctx.count+2+idx])
|
||||
return (p.vstack[p.vsp-p.ctx.count+2+idx])
|
||||
}
|
||||
|
||||
func (p *process_t) GetNumArgs() int {
|
||||
@ -97,7 +99,7 @@ func (p *process_t) GetNumArgs() int {
|
||||
}
|
||||
|
||||
func (p *process_t) Return(val string) {
|
||||
p.stack[p.sp-p.ctx.count] = unsafe.Pointer(&val)
|
||||
p.vstack[p.vsp-p.ctx.count] = unsafe.Pointer(&val)
|
||||
}
|
||||
|
||||
func (p *process_t) push_context(node *Cnode_t, container_node *Cnode_t) {
|
||||
@ -116,11 +118,11 @@ func (p *process_t) pop_context() (*Cnode_t, *Cnode_t) {
|
||||
|
||||
// clean up the unused part of the stack
|
||||
for i = 1; i < p.ctx.count; i++ {
|
||||
p.stack[p.sp-p.ctx.count+i] = nil
|
||||
p.vstack[p.vsp-p.ctx.count+i] = nil
|
||||
}
|
||||
|
||||
// pop off the cleaned arguments
|
||||
p.sp -= p.ctx.count - 1 // keep the return value in the stack
|
||||
p.vsp -= p.ctx.count - 1 // keep the return value in the stack
|
||||
p.ctx = p.ctx.parent_ctx
|
||||
|
||||
if p.ctx != nil {
|
||||
@ -144,7 +146,12 @@ func (interp *Interp) eval_node_child(container_node *Cnode_t) (*string, error)
|
||||
p.interp = interp
|
||||
stmt_node = container_node.child
|
||||
|
||||
fmt.Printf("START p.sp = %d\n", p.sp)
|
||||
//interp.dump_cnodes(container_node, true)
|
||||
//fmt.Printf("--------------\n")
|
||||
//interp.dump_cnodes(stmt_node, true)
|
||||
//fmt.Printf("--------------\n")
|
||||
|
||||
fmt.Printf("START p.sp = %d\n", p.vsp)
|
||||
for stmt_node != nil {
|
||||
if stmt_node.code != CNODE_STMT {
|
||||
panic("internal error - not statement node")
|
||||
@ -159,7 +166,7 @@ func (interp *Interp) eval_node_child(container_node *Cnode_t) (*string, error)
|
||||
for inner_node != nil {
|
||||
switch inner_node.code {
|
||||
case CNODE_BRACKET:
|
||||
|
||||
case CNODE_DQUOTE:
|
||||
if inner_node.child != nil {
|
||||
stmt_node = inner_node.child // first statement inside []
|
||||
p.push_context(stmt_node, inner_node)
|
||||
@ -170,9 +177,12 @@ func (interp *Interp) eval_node_child(container_node *Cnode_t) (*string, error)
|
||||
p.push_string_value("")
|
||||
}
|
||||
|
||||
case CNODE_BRACE:
|
||||
p.push_cnode_value(inner_node)
|
||||
|
||||
case CNODE_TEXT:
|
||||
|
||||
//fmt.Printf("XXXXXXXXXXXXXXXXXXXx[%s]\n", string(child_node.token))
|
||||
//fmt.Printf("XXXXXXXXXXXXXXXXXXXx[%s]\n", string(inner_node.token))
|
||||
err = p.push_string_value(string(inner_node.token))
|
||||
if err != nil {
|
||||
goto oops
|
||||
@ -184,10 +194,14 @@ func (interp *Interp) eval_node_child(container_node *Cnode_t) (*string, error)
|
||||
inner_node = inner_node.next
|
||||
}
|
||||
|
||||
//fmt.Printf("CALLING\n")
|
||||
err = p.call()
|
||||
if err != nil {
|
||||
goto oops
|
||||
fmt.Printf("CALLING for %d\n", p.ctx.parent_node.code)
|
||||
if p.ctx.parent_node.code == CNODE_STMT {
|
||||
err = p.call()
|
||||
if err != nil {
|
||||
goto oops
|
||||
}
|
||||
} else {
|
||||
fmt.Printf("DDDDDDDDDDDDBVVVVVVVVVVVDDDd\n")
|
||||
}
|
||||
|
||||
stmt_node, inner_node = p.pop_context()
|
||||
@ -200,7 +214,7 @@ func (interp *Interp) eval_node_child(container_node *Cnode_t) (*string, error)
|
||||
stmt_node = stmt_node.next
|
||||
}
|
||||
|
||||
fmt.Printf("END p.sp = %d\n", p.sp)
|
||||
fmt.Printf("END p.sp = %d\n", p.vsp)
|
||||
return v, nil
|
||||
|
||||
oops:
|
||||
@ -326,7 +340,7 @@ func (interp *Interp) eval_arg(p *process_t, pos int) (*string, error) {
|
||||
ptr = uintptr(p.GetArg(pos))
|
||||
if ptr&1 == 1 { // cnode
|
||||
ptr &= ^uintptr(1)
|
||||
interp.dump_cnodes((*Cnode_t)(unsafe.Pointer(ptr)), true)
|
||||
//interp.dump_cnodes((*Cnode_t)(unsafe.Pointer(ptr)), true)
|
||||
//return interp.eval_atom_node((*Cnode_t)(unsafe.Pointer(ptr)).child)
|
||||
return interp.eval_node_child((*Cnode_t)(unsafe.Pointer(ptr)))
|
||||
} else {
|
||||
|
@ -42,8 +42,8 @@ type context_t struct {
|
||||
|
||||
type process_t struct {
|
||||
interp *Interp
|
||||
stack [16]unsafe.Pointer // value stack - TODO: change size
|
||||
sp int
|
||||
vstack [16]unsafe.Pointer // value stack - TODO: change size
|
||||
vsp int
|
||||
ctx *context_t
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user