Skip to content

Commit

Permalink
重新添加prefixexp ':' Name args
Browse files Browse the repository at this point in the history
  • Loading branch information
lollipopkit committed Oct 1, 2022
1 parent 7c6fdd4 commit 3a72c40
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 16 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# CHANGELOG
**仅包含语言LK的变化**

## 0.1.3


## 0.1.2
- `Table`索引从`0`开始
- 去除以`'''` `"""`构造长String,当前仅支持``` `` ```构造
Expand Down
22 changes: 11 additions & 11 deletions compiler/parser/parse_prefix_exp.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func _finishPrefixExp(lexer *Lexer, exp Exp) Exp {
line, name := lexer.NextIdentifier() // Name
keyExp := &StringExp{line, name}
exp = &TableAccessExp{line, exp, keyExp}
case TOKEN_SEP_LPAREN, TOKEN_STRING: // prefixexp args
case TOKEN_SEP_LPAREN, TOKEN_STRING, TOKEN_SEP_COLON: // prefixexp args
exp = _finishFuncCallExp(lexer, exp)
default:
return exp
Expand All @@ -65,21 +65,21 @@ func _finishPrefixExp(lexer *Lexer, exp Exp) Exp {

// functioncall ::= prefixexp args | prefixexp ‘:’ Name args
func _finishFuncCallExp(lexer *Lexer, prefixExp Exp) *FuncCallExp {
//nameExp := _parseNameExp(lexer)
nameExp := _parseNameExp(lexer)
line := lexer.Line() // todo
args := _parseArgs(lexer)
lastLine := lexer.Line()
return &FuncCallExp{line, lastLine, prefixExp, nil, args}
return &FuncCallExp{line, lastLine, prefixExp, nameExp, args}
}

// func _parseNameExp(lexer *Lexer) *StringExp {
// if lexer.LookAhead() == TOKEN_SEP_COLON {
// lexer.NextToken()
// line, name := lexer.NextIdentifier()
// return &StringExp{line, name}
// }
// return nil
// }
func _parseNameExp(lexer *Lexer) *StringExp {
if lexer.LookAhead() == TOKEN_SEP_COLON {
lexer.NextToken()
line, name := lexer.NextIdentifier()
return &StringExp{line, name}
}
return nil
}

// args ::= ‘(’ [explist] ‘)’ | tableconstructor | LiteralString
func _parseArgs(lexer *Lexer) (args []Exp) {
Expand Down
18 changes: 13 additions & 5 deletions compiler/parser/parse_stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,13 @@ func _checkVar(lexer *Lexer, exp Exp) Exp {
// namelist ::= Name {‘,’ Name}
func parseFuncDefStat(lexer *Lexer) *AssignStat {
lexer.NextTokenOfKind(TOKEN_KW_FUNCTION) // function
fnExp := _parseFuncName(lexer) // funcname
fnExp, hasColon := _parseFuncName(lexer) // funcname
fdExp := parseFuncDefExp(lexer) // funcbody
if hasColon { // insert self
fdExp.ParList = append(fdExp.ParList, "")
copy(fdExp.ParList[1:], fdExp.ParList)
fdExp.ParList[0] = "self"
}

return &AssignStat{
LastLine: fdExp.Line,
Expand All @@ -261,17 +266,20 @@ func parseFuncDefStat(lexer *Lexer) *AssignStat {
}
}

// funcname ::= Name {‘.’ Name}
func _parseFuncName(lexer *Lexer) (exp Exp) {
// funcname ::= Name {‘.’ Name} [‘:’ Name]
func _parseFuncName(lexer *Lexer) (exp Exp, hasColon bool) {
line, name := lexer.NextIdentifier()
exp = &NameExp{line, name}

for lexer.LookAhead() == TOKEN_SEP_DOT {
switch lexer.LookAhead() {
case TOKEN_SEP_COLON:
hasColon = true
fallthrough
case TOKEN_SEP_DOT:
lexer.NextToken()
line, name := lexer.NextIdentifier()
idx := &StringExp{line, name}
exp = &TableAccessExp{line, exp, idx}
}

return
}
18 changes: 18 additions & 0 deletions test/object.lk
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Rectangle = {'area': 0, 'length': 0, 'breadth': 0}

fn Rectangle:new(o,length,breadth) {
o = o or {}
setmetatable(o, self)
self.__index = self
self.length = length or 0
self.breadth = breadth or 0
self.area = length*breadth;
rt o
}

fn Rectangle:printArea() {
print("矩形面积为 ", self.area)
}

shy rect = Rectangle:new(nil, 10, 20)
rect:printArea()

0 comments on commit 3a72c40

Please sign in to comment.