Skip to content

Commit

Permalink
Merge pull request #2106 from xushiwei/q
Browse files Browse the repository at this point in the history
pseudo: parseNames
  • Loading branch information
xushiwei authored Feb 15, 2025
2 parents 8a109dd + 1016ce2 commit 5fd7c0b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 33 deletions.
6 changes: 3 additions & 3 deletions demo/gop-parser/pseudo/ast.gop
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ type OutputStmt struct {

// InputStmt - 输入语句
//
// INPUT {变量名}
// INPUT {变量名1}, {变量名2}...
type InputStmt struct {
Name string
Names []string
}

// IfStmt - 条件语句
Expand All @@ -79,7 +79,7 @@ type WhileStmt struct {
Body []Stmt
}

// UntilStmt - Unti循环语句
// UntilStmt - Until循环语句
//
// REPEAT
// {语句块}
Expand Down
51 changes: 28 additions & 23 deletions demo/gop-parser/pseudo/parser.gop
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,28 @@ func parseType(s *scanner.Scanner) Type {
return &Ident{Name: name}
}

// {变量名1}, {变量名2}...
func parseNames(s *scanner.Scanner) (names []string, tok token.Token) {
for {
_, name := expect(s, token.IDENT) // {名称}
names = append(names, name)
_, tok, _ = s.scan()
if tok == token.COMMA { // ,
continue
}
return
}
}

// {常量名} <- {表达式}
func parseNameAndValue(rest string) (string, ast.Expr) {
s := scanner.new(rest, nil, 0)
_, name := expect(s, token.IDENT) // {名称}
pos, _ := expect(s, token.ARROW) // <-
value := parseExpr(rest[pos+1:]) // {表达式}
return name, value
}

// Expr - 表达式
func parseExpr(expr string) ast.Expr {
e, err := parser.parseExpr(expr)
Expand All @@ -80,31 +102,14 @@ func parseExpr(expr string) ast.Expr {
// DECLARE {变量名1}, {变量名2}... : {类型}
func parseVarStmt(rest string, lines []string) (Stmt, []string) {
s := scanner.new(rest, nil, 0)
var names []string
for {
_, name := expect(s, token.IDENT) // {名称}
names = append(names, name)
_, tok, _ := s.scan()
if tok == token.COMMA { // ,
continue
} else if tok == token.COLON { // :
break
} else {
panic("DECLARE: expect , or :")
}
names, tok := parseNames(s) // {变量名1}, {变量名2}...
if tok != token.COLON { // :
panic("DECLARE: expect , or :")
}
typ := parseType(s) // {类型}
return &VarStmt{Names: names, Type: typ}, lines[1:]
}

func parseNameAndValue(rest string) (string, ast.Expr) {
s := scanner.new(rest, nil, 0)
_, name := expect(s, token.IDENT) // {名称}
pos, _ := expect(s, token.ARROW) // <-
value := parseExpr(rest[pos+1:]) // {表达式}
return name, value
}

// ConstStmt - 常量声明语句
//
// CONSTANT {常量名} <- {表达式}
Expand All @@ -131,11 +136,11 @@ func parseOutputStmt(rest string, lines []string) (Stmt, []string) {

// InputStmt - 输入语句
//
// INPUT {变量名}
// INPUT {变量名1}, {变量名2}...
func parseInputStmt(rest string, lines []string) (Stmt, []string) {
s := scanner.new(rest, nil, 0)
_, name := expect(s, token.IDENT) // {变量名}
return &InputStmt{Name: name}, lines[1:]
names, _ := parseNames(s)
return &InputStmt{Names: names}, lines[1:]
}

// IfStmt - If 语句
Expand Down
15 changes: 8 additions & 7 deletions demo/gop-parser/pseudo/state.gox
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,15 @@ func execOutputStmt(stmt *OutputStmt) {
}

func execInputStmt(stmt *InputStmt) {
name := stmt.Name
oldv, ok := vars[name]
if !ok {
panic("undefined variable `${name}`")
for name <- stmt.Names {
oldv, ok := vars[name]
if !ok {
panic("undefined variable `${name}`")
}
v := reflect.new(reflect.typeOf(oldv))
fmt.scanln(v.Interface())!
vars[name] = v.elem.Interface()
}
v := reflect.new(reflect.typeOf(oldv))
fmt.scanln(v.Interface())!
vars[name] = v.elem.Interface()
}

func execIfStmt(stmt *IfStmt) {
Expand Down

0 comments on commit 5fd7c0b

Please sign in to comment.