Esempio n. 1
0
func (t *Tree) parseFuncExpr(pos int) ast.ExprNode {
	const context = "function expression"
	t.openScope()
	tok := t.expectOneOf(token.LPAREN, token.IDENT, context)

	var params []string
	if tok.Type() == token.LPAREN {
		next := t.peekNonSpace()
		if next.Type() != token.RPAREN {
			for {
				param := t.expect(token.IDENT, context)
				//if found nil then create new Object in scope else it is already in the scope
				if t.topScope.Lookup(param.Val()) == nil {
					obj := ast.NewObj(param.Val())
					t.topScope.Insert(obj)
				}
				fmt.Println(param.Val())
				params = append(params, param.Val())

				if next := t.peekNonSpace(); next.Type() != token.COMMA {
					break
				}
				t.nextNonSpace()
			}
			t.expect(token.RPAREN, context)
		} else {
			t.expect(token.RPAREN, context)
		}

	} else {
		if t.topScope.Lookup(tok.Val()) == nil {
			obj := ast.NewObj(tok.Val())
			t.topScope.Insert(obj)
		}
		fmt.Println(tok.Val())
		params = append(params, tok.Val())
	}

	t.expect(token.ARROW, context)
	body := t.parseExpr()
	tmpNode := ast.NewFunctionExpression(pos, params, body)
	t.closeScope()
	return tmpNode
}
Esempio n. 2
0
func (t *Tree) parseDefn() *ast.DefnNode {
	const context = "definition"

	iden := t.expect(token.IDENT, context)
	//if found nil then create new Object in scope else it is already in the scope
	if t.topScope.Lookup(iden.Val()) == nil {
		obj := ast.NewObj(iden.Val())
		t.topScope.Insert(obj)
	}

	t.expect(token.ASSIGN, context)
	exprNode := t.parseExpr()
	return ast.NewDefinition(iden.Pos(), iden.Val(), exprNode)
}
Esempio n. 3
0
func (t *Tree) parseFunc(pos int) ast.ExprNode {
	const context = "function statement"
	name := t.expect(token.IDENT, context)

	/*
		scope := t.topScope

		for {
			if scope.Lookup(name.Val()) != nil {
				t.errorf("Function already declared in upper scope", context)
			}

			scope = t.topScope.Outer

			if scope == nil {
				break
			}
		}
	*/

	fmt.Println("Parsing function definition")
	obj := ast.NewObj(name.Val())
	t.topScope.Insert(obj)

	t.openScope()
	var params []string
	tok := t.nextNonSpace()

	switch tok.Type() {
	case token.LPAREN:
		next := t.peekNonSpace()
		if next.Type() != token.RPAREN {
			for {
				param := t.expect(token.IDENT, context)
				//if found nil then create new Object in scope else it is already in the scope
				if t.topScope.Lookup(param.Val()) == nil {
					obj := ast.NewObj(param.Val())
					t.topScope.Insert(obj)
				}
				fmt.Println(param.Val())
				params = append(params, param.Val())

				if next := t.peekNonSpace(); next.Type() != token.COMMA {
					break
				}
				t.nextNonSpace()
			}
			t.expect(token.RPAREN, context)
		} else {
			t.expect(token.RPAREN, context)
		}
	case token.IDENT:
		if t.topScope.Lookup(tok.Val()) == nil {
			obj := ast.NewObj(tok.Val())
			t.topScope.Insert(obj)
		}
		fmt.Println(tok.Val())
		params = append(params, tok.Val())
	case token.ARROW:
		body := t.parseExpr()
		tmpNode := ast.NewFunction(pos, name.Val(), params, body)
		t.closeScope()
		return tmpNode
	default:
		t.unexpected(tok, context)
	}

	t.expect(token.ARROW, context)
	body := t.parseExpr()
	tmpNode := ast.NewFunction(pos, name.Val(), params, body)
	t.closeScope()

	return tmpNode
}