Browse Source

Work on OOP-style methods, with comma operator, but the precedence is wrong.

Beoran 4 years ago
parent
commit
9a6e3748b9
3 changed files with 11 additions and 11 deletions
  1. 3 1
      builtin.go
  2. 1 3
      door.go
  3. 7 7
      parser.go

+ 3 - 1
builtin.go

@@ -427,6 +427,8 @@ func exit(vm *VM, val ...Value) [] Value {
 }
 
 func comma(vm * VM, val ...Value) []Value {
+    fmt.Printf("Comma arguments: %v\n", val)
+    
     if len(val) < 2 {
         return Fail(fmt.Errorf("Need at least 2 arguments"))
     }
@@ -441,7 +443,7 @@ func comma(vm * VM, val ...Value) []Value {
     } else if name, isName := command.(WordValue) ; isName {
         return vm.CallNamed(name.String(), val...)
     } else {
-        return Fail(fmt.Errorf("Not callable: %v"))
+        return Fail(fmt.Errorf("Not callable: %v", command))
     }
 }
 

+ 1 - 3
door.go

@@ -4,13 +4,12 @@ import "fmt"
 
 // Door is an example of how to wrap a custom type in Muesli.
 type Door struct {
-	RedispatchCallable
 	name string
 	locked bool
 	opened bool
 }
 
-var _ Callable = &Door{}
+var _ Value = &Door{}
 
 const DoorType = TypeValue("Door")
 
@@ -45,7 +44,6 @@ func door(vm *VM, val ...Value) [] Value {
 		return Fail(err)
 	}
 	d := &Door{name: name, locked: locked, opened:false}
-	d.RedispatchCallable = NewRedispatchCallable("Door", d)
 	return Ok(d)
 }
 

+ 7 - 7
parser.go

@@ -596,23 +596,23 @@ func (parser *Parser) ParseChain() *Ast {
     // for easy composition
     var chain *Ast
     
-    for i, j := len(expressions) - 1, len(operators) - 1 ; i >= 0 && j >= 0 ; i, j = i - 1 , j - 1 {
+    for i, j := 0, 0 ; i < len(expressions) && j < len(operators) ; i, j = i + 1 , j + 1 {
         expression := expressions[i]
         oper := operators[j]
         if chain == nil {
-            expression2 := expressions[i-1]
-            subst2 := NewAstWithToken(AstKindParenthesis, oper)
-            subst2.AppendChildren(expression)
+            expression2 := expressions[i+1]
             subst1 := NewAstWithToken(AstKindParenthesis, oper)
-            subst1.AppendChildren(expression2)
+            subst1.AppendChildren(expression)
+            subst2 := NewAstWithToken(AstKindParenthesis, oper)
+            subst2.AppendChildren(expression2)
             chain = NewAstWithToken(AstKindOperation, oper)
             chain.AppendChildren(subst1, subst2)
-            i--
+            i++
         } else {
             subst := NewAstWithToken(AstKindParenthesis, oper)
             subst.AppendChildren(expression)
             newChain := NewAstWithToken(AstKindOperation, oper)
-            newChain.AppendChildren(subst, chain)
+            newChain.AppendChildren(chain, subst)
             chain = newChain
         }
     }