diff --git a/ast/ast.go b/ast/ast.go index 43fb147..3f63786 100644 --- a/ast/ast.go +++ b/ast/ast.go @@ -334,15 +334,18 @@ func NewVariable(curr_type, id, dim1, dim2, rows Attrib) ([]*tables.VarRow, erro if !tok_ok { return nil, errors.New("Problem in casting id token") } - new_dim1, _ := dim1.(int) - new_dim2, _ := dim1.(int) + new_dim1 := 1 + if dim1 != nil { + curr_dim, _ := dim1.(*token.Token).Int32Value() + new_dim1 = int(curr_dim) + } // create variable row row := &tables.VarRow{} // TODO Constructor for VarRow if curr_type != nil { row.SetType(curr_type.(types.CoreType)) } - row.SetDim1(new_dim1) - row.SetDim2(new_dim2) + row.SetDim1(int(new_dim1)) + // row.SetDim2(new_dim2) // set values to varibale row row.SetId(string(tok.Lit)) row.SetToken(tok) @@ -360,7 +363,8 @@ func NewVariable(curr_type, id, dim1, dim2, rows Attrib) ([]*tables.VarRow, erro } } else { // choose global context - current_address, err_addr = vmemory.NextGlobalTemp(row.Type()) + fmt.Println("New global temp var"); + current_address, err_addr = vmemory.NextGlobalTemp(row.Type(),new_dim1) if err_addr != nil { fmt.Println("Error in new global temp: ", err_addr) } @@ -656,7 +660,7 @@ func createBinaryQuadruple(new_op semantic.Operation) []quadruples.Cuadruplo { } else { // choose global context fmt.Println("No global current scope") - current_address, err_addr = vmemory.NextGlobalTemp(cube_type) + current_address, err_addr = vmemory.NextGlobalTemp(cube_type,1) if err_addr != nil { fmt.Println("Error in new global temp: ", err_addr) } @@ -844,7 +848,7 @@ func GetIdDimConst(id, dim1, dim2 Attrib) (*Constant, error) { // TODO (Access id address from vartable scope instead of curr address) // TODO (Check dimensions) // calculate current address occuppied in context - current_address, _ := vmemory.NextGlobalTemp(types.Ids) // TODO Check Types (Validate type with semantic cube) + current_address, _ := vmemory.NextGlobalTemp(types.Ids,1) // TODO Check Types (Validate type with semantic cube) return &Constant{string(val.Lit), val, types.Ids, current_address}, nil } diff --git a/bar.bnf b/bar.bnf index d530c19..63ee243 100644 --- a/bar.bnf +++ b/bar.bnf @@ -40,14 +40,14 @@ ATR_SIMP1 : empty | ATR_SIMP ATR_SIMP1 ; DV : "variables" "{" DV1 "}" << $2, nil >>; DV1 : empty | DV2 ";" DV1 << ast.NewBlockVariables($0,$2) >> ; DV2 : TIP_COMP id DV4 | DV6 DV5 << ast.NewTypeVariables($0,$1) >>; -DV3 : "[" cte_i "]" << ast.NewIntConst($1) >> ; +DV3 : "[" cte_i "]" << $1,nil >> ; DV4 : empty | "," id DV4 ; DV5 : empty - | "," id DV5<< ast.NewVariable(nil,$1,0,0, $2) >> - | "," id DV3 DV5<< ast.NewVariable(nil,$1,$2,0, $3) >> + | "," id DV5<< ast.NewVariable(nil,$1,nil,nil, $2) >> + | "," id DV3 DV5<< ast.NewVariable(nil,$1,$2,nil, $3) >> | "," id DV3 DV3 DV5<< ast.NewVariable(nil,$1,$2,$3, $4) >> ; -DV6 : TIP_SIMP id << ast.NewVariable($0,$1,0,0, nil) >> - | TIP_SIMP id DV3 << ast.NewVariable($0,$1,$2,0, nil) >> +DV6 : TIP_SIMP id << ast.NewVariable($0,$1,nil,nil, nil) >> + | TIP_SIMP id DV3 << ast.NewVariable($0,$1,$2,nil, nil) >> | TIP_SIMP id DV3 DV3 << ast.NewVariable($0,$1,$2,$3, nil) >> ; TIP_SIMP : "int" << types.Integer, nil >> diff --git a/gocc/LR1_conflicts.txt b/gocc/LR1_conflicts.txt index e3d6f62..3ff9cfd 100644 --- a/gocc/LR1_conflicts.txt +++ b/gocc/LR1_conflicts.txt @@ -28,15 +28,15 @@ Reduce(20:ATR_SIMP1 : empty << >>) Shift(20) symbol: char - Shift(21) Reduce(20:ATR_SIMP1 : empty << >>) + Shift(21) symbol: float Reduce(20:ATR_SIMP1 : empty << >>) Shift(22) S687 symbol: int - Reduce(20:ATR_SIMP1 : empty << >>) Shift(20) + Reduce(20:ATR_SIMP1 : empty << >>) symbol: char Reduce(20:ATR_SIMP1 : empty << >>) Shift(21) @@ -45,8 +45,8 @@ Shift(22) S693 symbol: int - Shift(20) Reduce(20:ATR_SIMP1 : empty << >>) + Shift(20) symbol: char Reduce(20:ATR_SIMP1 : empty << >>) Shift(21) @@ -54,16 +54,16 @@ Reduce(20:ATR_SIMP1 : empty << >>) Shift(22) S695 + symbol: int + Shift(20) + Reduce(20:ATR_SIMP1 : empty << >>) symbol: char Reduce(20:ATR_SIMP1 : empty << >>) Shift(21) symbol: float Shift(22) Reduce(20:ATR_SIMP1 : empty << >>) - symbol: int - Reduce(20:ATR_SIMP1 : empty << >>) - Shift(20) S697 symbol: function - Reduce(48:F4 : empty << >>) Shift(344) + Reduce(48:F4 : empty << >>) diff --git a/gocc/errors/errors.go b/gocc/errors/errors.go index 258283f..3339c29 100644 --- a/gocc/errors/errors.go +++ b/gocc/errors/errors.go @@ -4,9 +4,7 @@ package errors import ( "fmt" - "strconv" "strings" - "unicode" "github.com/uliseslugo/ilusa_pp/gocc/token" ) @@ -24,85 +22,35 @@ type Error struct { func (e *Error) String() string { w := new(strings.Builder) + fmt.Fprintf(w, "Error") if e.Err != nil { - fmt.Fprintln(w, "Error ", e.Err) + fmt.Fprintf(w, " %s\n", e.Err) } else { - fmt.Fprintln(w, "Error") + fmt.Fprintf(w, "\n") } fmt.Fprintf(w, "Token: type=%d, lit=%s\n", e.ErrorToken.Type, e.ErrorToken.Lit) fmt.Fprintf(w, "Pos: offset=%d, line=%d, column=%d\n", e.ErrorToken.Pos.Offset, e.ErrorToken.Pos.Line, e.ErrorToken.Pos.Column) - fmt.Fprint(w, "Expected one of: ") + fmt.Fprintf(w, "Expected one of: ") for _, sym := range e.ExpectedTokens { - fmt.Fprint(w, string(sym), " ") + fmt.Fprintf(w, "%s ", sym) } - fmt.Fprintln(w, "ErrorSymbol:") + fmt.Fprintf(w, "ErrorSymbol:\n") for _, sym := range e.ErrorSymbols { fmt.Fprintf(w, "%v\n", sym) } - return w.String() } -func DescribeExpected(tokens []string) string { - switch len(tokens) { - case 0: - return "unexpected additional tokens" - - case 1: - return "expected " + tokens[0] - - case 2: - return "expected either " + tokens[0] + " or " + tokens[1] - - case 3: - // Oxford-comma rules require more than 3 items in a list for the - // comma to appear before the 'or' - return fmt.Sprintf("expected one of %s, %s or %s", tokens[0], tokens[1], tokens[2]) - - default: - // Oxford-comma separated alternatives list. - tokens = append(tokens[:len(tokens)-1], "or "+tokens[len(tokens)-1]) - return "expected one of " + strings.Join(tokens, ", ") - } -} - -func DescribeToken(tok *token.Token) string { - switch tok.Type { - case token.INVALID: - return fmt.Sprintf("unknown/invalid token %q", tok.Lit) - case token.EOF: - return "end-of-file" - default: - return fmt.Sprintf("%q", tok.Lit) - } -} - func (e *Error) Error() string { - // identify the line and column of the error in 'gnu' style so it can be understood - // by editors and IDEs; user will need to prefix it with a filename. - text := fmt.Sprintf("%d:%d: error: ", e.ErrorToken.Pos.Line, e.ErrorToken.Pos.Column) - - // See if the error token can provide us with the filename. - switch src := e.ErrorToken.Pos.Context.(type) { - case token.Sourcer: - text = src.Source() + ":" + text - } - + w := new(strings.Builder) + fmt.Fprintf(w, "Error in S%d: %s, %s", e.StackTop, token.TokMap.TokenString(e.ErrorToken), e.ErrorToken.Pos.String()) if e.Err != nil { - // Custom error specified, e.g. by << nil, errors.New("missing newline") >> - text += e.Err.Error() + fmt.Fprintf(w, ": %+v", e.Err) } else { - tokens := make([]string, len(e.ExpectedTokens)) - for idx, token := range e.ExpectedTokens { - if !unicode.IsLetter(rune(token[0])) { - token = strconv.Quote(token) - } - tokens[idx] = token + fmt.Fprintf(w, ", expected one of: ") + for _, expected := range e.ExpectedTokens { + fmt.Fprintf(w, "%s ", expected) } - text += DescribeExpected(tokens) - actual := DescribeToken(e.ErrorToken) - text += fmt.Sprintf("; got: %s", actual) } - - return text + return w.String() } diff --git a/gocc/lexer/lexer.go b/gocc/lexer/lexer.go index ec2a655..27afeed 100644 --- a/gocc/lexer/lexer.go +++ b/gocc/lexer/lexer.go @@ -16,50 +16,35 @@ const ( ) type Lexer struct { - src []byte - pos int - line int - column int - Context token.Context + src []byte + pos int + line int + column int } func NewLexer(src []byte) *Lexer { lexer := &Lexer{ - src: src, - pos: 0, - line: 1, - column: 1, - Context: nil, + src: src, + pos: 0, + line: 1, + column: 1, } return lexer } -// SourceContext is a simple instance of a token.Context which -// contains the name of the source file. -type SourceContext struct { - Filepath string -} - -func (s *SourceContext) Source() string { - return s.Filepath -} - func NewLexerFile(fpath string) (*Lexer, error) { src, err := ioutil.ReadFile(fpath) if err != nil { return nil, err } - lexer := NewLexer(src) - lexer.Context = &SourceContext{Filepath: fpath} - return lexer, nil + return NewLexer(src), nil } func (l *Lexer) Scan() (tok *token.Token) { - tok = &token.Token{} + tok = new(token.Token) if l.pos >= len(l.src) { tok.Type = token.EOF tok.Pos.Offset, tok.Pos.Line, tok.Pos.Column = l.pos, l.line, l.column - tok.Pos.Context = l.Context return } start, startLine, startColumn, end := l.pos, l.line, l.column, 0 @@ -118,7 +103,6 @@ func (l *Lexer) Scan() (tok *token.Token) { tok.Lit = []byte{} } tok.Pos.Offset, tok.Pos.Line, tok.Pos.Column = start, startLine, startColumn - tok.Pos.Context = l.Context return } diff --git a/gocc/parser/parser.go b/gocc/parser/parser.go index c44d33b..942c01a 100644 --- a/gocc/parser/parser.go +++ b/gocc/parser/parser.go @@ -91,7 +91,6 @@ type Parser struct { stack *stack nextToken *token.Token pos int - Context Context } type Scanner interface { @@ -203,7 +202,7 @@ func (p *Parser) Parse(scanner Scanner) (res interface{}, err error) { p.nextToken = scanner.Scan() case reduce: prod := productionsTable[int(act)] - attrib, err := prod.ReduceFunc(p.stack.popN(prod.NumSymbols), p.Context) + attrib, err := prod.ReduceFunc(p.stack.popN(prod.NumSymbols)) if err != nil { return nil, p.newError(err) } else { diff --git a/gocc/parser/productionstable.go b/gocc/parser/productionstable.go index d43d1ae..b941255 100644 --- a/gocc/parser/productionstable.go +++ b/gocc/parser/productionstable.go @@ -14,7 +14,7 @@ type ( NTType int Index int NumSymbols int - ReduceFunc func([]Attrib, interface{}) (Attrib, error) + ReduceFunc func([]Attrib) (Attrib, error) } Attrib interface { } @@ -27,7 +27,7 @@ var productionsTable = ProdTab{ NTType: 0, Index: 0, NumSymbols: 1, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return X[0], nil }, }, @@ -37,7 +37,7 @@ var productionsTable = ProdTab{ NTType: 1, Index: 1, NumSymbols: 7, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return ast.NewProgram(X[1],X[5],X[6]) }, }, @@ -47,7 +47,7 @@ var productionsTable = ProdTab{ NTType: 2, Index: 2, NumSymbols: 0, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return nil, nil }, }, @@ -57,7 +57,7 @@ var productionsTable = ProdTab{ NTType: 2, Index: 3, NumSymbols: 1, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return X[0], nil }, }, @@ -67,7 +67,7 @@ var productionsTable = ProdTab{ NTType: 3, Index: 4, NumSymbols: 0, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return nil, nil }, }, @@ -77,7 +77,7 @@ var productionsTable = ProdTab{ NTType: 3, Index: 5, NumSymbols: 1, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return ast.GlobalVarDec(X[0]) }, }, @@ -87,7 +87,7 @@ var productionsTable = ProdTab{ NTType: 4, Index: 6, NumSymbols: 0, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return nil, nil }, }, @@ -97,7 +97,7 @@ var productionsTable = ProdTab{ NTType: 4, Index: 7, NumSymbols: 1, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return X[0], nil }, }, @@ -107,7 +107,7 @@ var productionsTable = ProdTab{ NTType: 5, Index: 8, NumSymbols: 6, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return X[4], nil }, }, @@ -117,7 +117,7 @@ var productionsTable = ProdTab{ NTType: 6, Index: 9, NumSymbols: 0, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return nil, nil }, }, @@ -127,7 +127,7 @@ var productionsTable = ProdTab{ NTType: 6, Index: 10, NumSymbols: 2, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return ast.NewStatements(X[0],X[1]) }, }, @@ -137,7 +137,7 @@ var productionsTable = ProdTab{ NTType: 7, Index: 11, NumSymbols: 11, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return ast.NewClass(X[1]) }, }, @@ -147,7 +147,7 @@ var productionsTable = ProdTab{ NTType: 8, Index: 12, NumSymbols: 0, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return nil, nil }, }, @@ -157,7 +157,7 @@ var productionsTable = ProdTab{ NTType: 8, Index: 13, NumSymbols: 2, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return X[0], nil }, }, @@ -167,7 +167,7 @@ var productionsTable = ProdTab{ NTType: 9, Index: 14, NumSymbols: 0, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return nil, nil }, }, @@ -177,7 +177,7 @@ var productionsTable = ProdTab{ NTType: 9, Index: 15, NumSymbols: 5, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return X[0], nil }, }, @@ -187,7 +187,7 @@ var productionsTable = ProdTab{ NTType: 10, Index: 16, NumSymbols: 0, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return nil, nil }, }, @@ -197,7 +197,7 @@ var productionsTable = ProdTab{ NTType: 10, Index: 17, NumSymbols: 2, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return X[0], nil }, }, @@ -207,7 +207,7 @@ var productionsTable = ProdTab{ NTType: 11, Index: 18, NumSymbols: 9, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return X[0], nil }, }, @@ -217,7 +217,7 @@ var productionsTable = ProdTab{ NTType: 12, Index: 19, NumSymbols: 5, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return X[0], nil }, }, @@ -227,7 +227,7 @@ var productionsTable = ProdTab{ NTType: 13, Index: 20, NumSymbols: 0, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return nil, nil }, }, @@ -237,7 +237,7 @@ var productionsTable = ProdTab{ NTType: 13, Index: 21, NumSymbols: 2, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return X[0], nil }, }, @@ -247,7 +247,7 @@ var productionsTable = ProdTab{ NTType: 14, Index: 22, NumSymbols: 4, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return X[2], nil }, }, @@ -257,7 +257,7 @@ var productionsTable = ProdTab{ NTType: 15, Index: 23, NumSymbols: 0, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return nil, nil }, }, @@ -267,7 +267,7 @@ var productionsTable = ProdTab{ NTType: 15, Index: 24, NumSymbols: 3, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return ast.NewBlockVariables(X[0],X[2]) }, }, @@ -277,7 +277,7 @@ var productionsTable = ProdTab{ NTType: 16, Index: 25, NumSymbols: 3, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return X[0], nil }, }, @@ -287,18 +287,18 @@ var productionsTable = ProdTab{ NTType: 16, Index: 26, NumSymbols: 2, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return ast.NewTypeVariables(X[0],X[1]) }, }, ProdTabEntry{ - String: `DV3 : "[" cte_i "]" << ast.NewIntConst(X[1]) >>`, + String: `DV3 : "[" cte_i "]" << X[1],nil >>`, Id: "DV3", NTType: 17, Index: 27, NumSymbols: 3, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { - return ast.NewIntConst(X[1]) + ReduceFunc: func(X []Attrib) (Attrib, error) { + return X[1],nil }, }, ProdTabEntry{ @@ -307,7 +307,7 @@ var productionsTable = ProdTab{ NTType: 18, Index: 28, NumSymbols: 0, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return nil, nil }, }, @@ -317,7 +317,7 @@ var productionsTable = ProdTab{ NTType: 18, Index: 29, NumSymbols: 3, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return X[0], nil }, }, @@ -327,28 +327,28 @@ var productionsTable = ProdTab{ NTType: 19, Index: 30, NumSymbols: 0, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return nil, nil }, }, ProdTabEntry{ - String: `DV5 : "," id DV5 << ast.NewVariable(nil,X[1],0,0, X[2]) >>`, + String: `DV5 : "," id DV5 << ast.NewVariable(nil,X[1],nil,nil, X[2]) >>`, Id: "DV5", NTType: 19, Index: 31, NumSymbols: 3, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { - return ast.NewVariable(nil,X[1],0,0, X[2]) + ReduceFunc: func(X []Attrib) (Attrib, error) { + return ast.NewVariable(nil,X[1],nil,nil, X[2]) }, }, ProdTabEntry{ - String: `DV5 : "," id DV3 DV5 << ast.NewVariable(nil,X[1],X[2],0, X[3]) >>`, + String: `DV5 : "," id DV3 DV5 << ast.NewVariable(nil,X[1],X[2],nil, X[3]) >>`, Id: "DV5", NTType: 19, Index: 32, NumSymbols: 4, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { - return ast.NewVariable(nil,X[1],X[2],0, X[3]) + ReduceFunc: func(X []Attrib) (Attrib, error) { + return ast.NewVariable(nil,X[1],X[2],nil, X[3]) }, }, ProdTabEntry{ @@ -357,28 +357,28 @@ var productionsTable = ProdTab{ NTType: 19, Index: 33, NumSymbols: 5, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return ast.NewVariable(nil,X[1],X[2],X[3], X[4]) }, }, ProdTabEntry{ - String: `DV6 : TIP_SIMP id << ast.NewVariable(X[0],X[1],0,0, nil) >>`, + String: `DV6 : TIP_SIMP id << ast.NewVariable(X[0],X[1],nil,nil, nil) >>`, Id: "DV6", NTType: 20, Index: 34, NumSymbols: 2, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { - return ast.NewVariable(X[0],X[1],0,0, nil) + ReduceFunc: func(X []Attrib) (Attrib, error) { + return ast.NewVariable(X[0],X[1],nil,nil, nil) }, }, ProdTabEntry{ - String: `DV6 : TIP_SIMP id DV3 << ast.NewVariable(X[0],X[1],X[2],0, nil) >>`, + String: `DV6 : TIP_SIMP id DV3 << ast.NewVariable(X[0],X[1],X[2],nil, nil) >>`, Id: "DV6", NTType: 20, Index: 35, NumSymbols: 3, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { - return ast.NewVariable(X[0],X[1],X[2],0, nil) + ReduceFunc: func(X []Attrib) (Attrib, error) { + return ast.NewVariable(X[0],X[1],X[2],nil, nil) }, }, ProdTabEntry{ @@ -387,7 +387,7 @@ var productionsTable = ProdTab{ NTType: 20, Index: 36, NumSymbols: 4, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return ast.NewVariable(X[0],X[1],X[2],X[3], nil) }, }, @@ -397,7 +397,7 @@ var productionsTable = ProdTab{ NTType: 21, Index: 37, NumSymbols: 1, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return types.Integer, nil }, }, @@ -407,7 +407,7 @@ var productionsTable = ProdTab{ NTType: 21, Index: 38, NumSymbols: 1, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return types.Char, nil }, }, @@ -417,7 +417,7 @@ var productionsTable = ProdTab{ NTType: 21, Index: 39, NumSymbols: 1, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return types.Float, nil }, }, @@ -427,7 +427,7 @@ var productionsTable = ProdTab{ NTType: 22, Index: 40, NumSymbols: 1, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return X[0], nil }, }, @@ -437,7 +437,7 @@ var productionsTable = ProdTab{ NTType: 23, Index: 41, NumSymbols: 15, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return ast.NewFunction(X[2],X[4],X[7],X[10],X[11],X[14]) }, }, @@ -447,7 +447,7 @@ var productionsTable = ProdTab{ NTType: 24, Index: 42, NumSymbols: 1, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return ast.ResetLocalMemory() }, }, @@ -457,7 +457,7 @@ var productionsTable = ProdTab{ NTType: 24, Index: 43, NumSymbols: 1, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return ast.ResetLocalMemory() }, }, @@ -467,7 +467,7 @@ var productionsTable = ProdTab{ NTType: 25, Index: 44, NumSymbols: 0, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return nil, nil }, }, @@ -477,7 +477,7 @@ var productionsTable = ProdTab{ NTType: 25, Index: 45, NumSymbols: 3, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return ast.NewFunctionAttrib(X[0],X[1],X[2]) }, }, @@ -487,7 +487,7 @@ var productionsTable = ProdTab{ NTType: 26, Index: 46, NumSymbols: 0, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return nil, nil }, }, @@ -497,7 +497,7 @@ var productionsTable = ProdTab{ NTType: 26, Index: 47, NumSymbols: 4, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return ast.NewFunctionAttrib(X[1],X[2],X[3]) }, }, @@ -507,7 +507,7 @@ var productionsTable = ProdTab{ NTType: 27, Index: 48, NumSymbols: 0, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return nil, nil }, }, @@ -517,7 +517,7 @@ var productionsTable = ProdTab{ NTType: 27, Index: 49, NumSymbols: 1, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return X[0], nil }, }, @@ -527,7 +527,7 @@ var productionsTable = ProdTab{ NTType: 28, Index: 50, NumSymbols: 1, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return X[0], nil }, }, @@ -537,7 +537,7 @@ var productionsTable = ProdTab{ NTType: 28, Index: 51, NumSymbols: 1, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return X[0], nil }, }, @@ -547,7 +547,7 @@ var productionsTable = ProdTab{ NTType: 28, Index: 52, NumSymbols: 1, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return ast.FinishOutput(X[0]) }, }, @@ -557,7 +557,7 @@ var productionsTable = ProdTab{ NTType: 28, Index: 53, NumSymbols: 1, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return ast.FinishInput(X[0]) }, }, @@ -567,7 +567,7 @@ var productionsTable = ProdTab{ NTType: 28, Index: 54, NumSymbols: 1, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return X[0], nil }, }, @@ -577,7 +577,7 @@ var productionsTable = ProdTab{ NTType: 28, Index: 55, NumSymbols: 1, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return X[0], nil }, }, @@ -587,7 +587,7 @@ var productionsTable = ProdTab{ NTType: 28, Index: 56, NumSymbols: 1, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return X[0], nil }, }, @@ -597,7 +597,7 @@ var productionsTable = ProdTab{ NTType: 29, Index: 57, NumSymbols: 4, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return ast.NewAssignation(X[0],X[2]) }, }, @@ -607,7 +607,7 @@ var productionsTable = ProdTab{ NTType: 30, Index: 58, NumSymbols: 5, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return ast.NewFunctionCall(X[0], X[2]) }, }, @@ -617,7 +617,7 @@ var productionsTable = ProdTab{ NTType: 31, Index: 59, NumSymbols: 0, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return nil, nil }, }, @@ -627,7 +627,7 @@ var productionsTable = ProdTab{ NTType: 31, Index: 60, NumSymbols: 2, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return X[0], nil }, }, @@ -637,7 +637,7 @@ var productionsTable = ProdTab{ NTType: 32, Index: 61, NumSymbols: 0, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return nil, nil }, }, @@ -647,7 +647,7 @@ var productionsTable = ProdTab{ NTType: 32, Index: 62, NumSymbols: 3, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return X[0], nil }, }, @@ -657,7 +657,7 @@ var productionsTable = ProdTab{ NTType: 33, Index: 63, NumSymbols: 5, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return ast.Return(X[2]) }, }, @@ -667,7 +667,7 @@ var productionsTable = ProdTab{ NTType: 34, Index: 64, NumSymbols: 6, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return ast.NewInput(X[2],X[3]) }, }, @@ -677,7 +677,7 @@ var productionsTable = ProdTab{ NTType: 35, Index: 65, NumSymbols: 0, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return nil, nil }, }, @@ -687,7 +687,7 @@ var productionsTable = ProdTab{ NTType: 35, Index: 66, NumSymbols: 3, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return ast.NewInput(X[1],X[2]) }, }, @@ -697,7 +697,7 @@ var productionsTable = ProdTab{ NTType: 36, Index: 67, NumSymbols: 6, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return ast.NewOutput(X[2], X[3]) }, }, @@ -707,7 +707,7 @@ var productionsTable = ProdTab{ NTType: 37, Index: 68, NumSymbols: 1, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return X[0], nil }, }, @@ -717,7 +717,7 @@ var productionsTable = ProdTab{ NTType: 37, Index: 69, NumSymbols: 1, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return X[0], nil }, }, @@ -727,7 +727,7 @@ var productionsTable = ProdTab{ NTType: 38, Index: 70, NumSymbols: 0, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return nil, nil }, }, @@ -737,7 +737,7 @@ var productionsTable = ProdTab{ NTType: 38, Index: 71, NumSymbols: 3, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return ast.NewOutput(X[1], X[2]) }, }, @@ -747,7 +747,7 @@ var productionsTable = ProdTab{ NTType: 39, Index: 72, NumSymbols: 9, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return ast.NewIf(X[2],X[5],X[6],X[8]) }, }, @@ -757,7 +757,7 @@ var productionsTable = ProdTab{ NTType: 40, Index: 73, NumSymbols: 0, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return nil, nil }, }, @@ -767,7 +767,7 @@ var productionsTable = ProdTab{ NTType: 40, Index: 74, NumSymbols: 5, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return ast.NewElse(X[2],X[3]) }, }, @@ -777,7 +777,7 @@ var productionsTable = ProdTab{ NTType: 41, Index: 75, NumSymbols: 4, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return ast.NewWhile(X[2],X[3]) }, }, @@ -787,7 +787,7 @@ var productionsTable = ProdTab{ NTType: 41, Index: 76, NumSymbols: 8, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return X[0], nil }, }, @@ -797,7 +797,7 @@ var productionsTable = ProdTab{ NTType: 42, Index: 77, NumSymbols: 5, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return ast.LoopStatements(X[2],X[3]) }, }, @@ -807,7 +807,7 @@ var productionsTable = ProdTab{ NTType: 43, Index: 78, NumSymbols: 3, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return ast.GetIdDimConst(X[0], X[1], X[2]) }, }, @@ -817,7 +817,7 @@ var productionsTable = ProdTab{ NTType: 44, Index: 79, NumSymbols: 0, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return nil, nil }, }, @@ -827,7 +827,7 @@ var productionsTable = ProdTab{ NTType: 44, Index: 80, NumSymbols: 3, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return X[1], nil }, }, @@ -837,7 +837,7 @@ var productionsTable = ProdTab{ NTType: 45, Index: 81, NumSymbols: 2, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return ast.NewExpression(X[0],X[1]) }, }, @@ -847,7 +847,7 @@ var productionsTable = ProdTab{ NTType: 46, Index: 82, NumSymbols: 0, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return nil, nil }, }, @@ -857,7 +857,7 @@ var productionsTable = ProdTab{ NTType: 46, Index: 83, NumSymbols: 2, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return ast.NewOperation(X[0],X[1]) }, }, @@ -867,7 +867,7 @@ var productionsTable = ProdTab{ NTType: 47, Index: 84, NumSymbols: 2, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return ast.NewExpression(X[0],X[1]) }, }, @@ -877,7 +877,7 @@ var productionsTable = ProdTab{ NTType: 48, Index: 85, NumSymbols: 0, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return nil, nil }, }, @@ -887,7 +887,7 @@ var productionsTable = ProdTab{ NTType: 48, Index: 86, NumSymbols: 2, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return ast.NewOperation(X[0],X[1]) }, }, @@ -897,7 +897,7 @@ var productionsTable = ProdTab{ NTType: 49, Index: 87, NumSymbols: 2, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return ast.NewExpression(X[0],X[1]) }, }, @@ -907,7 +907,7 @@ var productionsTable = ProdTab{ NTType: 50, Index: 88, NumSymbols: 0, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return nil, nil }, }, @@ -917,7 +917,7 @@ var productionsTable = ProdTab{ NTType: 50, Index: 89, NumSymbols: 2, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return ast.NewOperation(X[0],X[1]) }, }, @@ -927,7 +927,7 @@ var productionsTable = ProdTab{ NTType: 50, Index: 90, NumSymbols: 2, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return ast.NewOperation(X[0],X[1]) }, }, @@ -937,7 +937,7 @@ var productionsTable = ProdTab{ NTType: 51, Index: 91, NumSymbols: 2, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return ast.NewExpression(X[0],X[1]) }, }, @@ -947,7 +947,7 @@ var productionsTable = ProdTab{ NTType: 52, Index: 92, NumSymbols: 0, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return nil, nil }, }, @@ -957,7 +957,7 @@ var productionsTable = ProdTab{ NTType: 52, Index: 93, NumSymbols: 2, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return ast.NewOperation(X[0],X[1]) }, }, @@ -967,7 +967,7 @@ var productionsTable = ProdTab{ NTType: 52, Index: 94, NumSymbols: 2, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return ast.NewOperation(X[0],X[1]) }, }, @@ -977,7 +977,7 @@ var productionsTable = ProdTab{ NTType: 53, Index: 95, NumSymbols: 3, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return X[0], nil }, }, @@ -987,7 +987,7 @@ var productionsTable = ProdTab{ NTType: 53, Index: 96, NumSymbols: 1, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return X[0], nil }, }, @@ -997,7 +997,7 @@ var productionsTable = ProdTab{ NTType: 53, Index: 97, NumSymbols: 2, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return ast.NewOperation(X[0],X[1]) }, }, @@ -1007,7 +1007,7 @@ var productionsTable = ProdTab{ NTType: 53, Index: 98, NumSymbols: 2, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return ast.NewOperation(X[0],X[1]) }, }, @@ -1017,7 +1017,7 @@ var productionsTable = ProdTab{ NTType: 54, Index: 99, NumSymbols: 1, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return ast.NewIdConst(X[0]) }, }, @@ -1027,7 +1027,7 @@ var productionsTable = ProdTab{ NTType: 54, Index: 100, NumSymbols: 1, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return ast.NewIntConst(X[0]) }, }, @@ -1037,7 +1037,7 @@ var productionsTable = ProdTab{ NTType: 54, Index: 101, NumSymbols: 1, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return ast.NewFloatConst(X[0]) }, }, @@ -1047,7 +1047,7 @@ var productionsTable = ProdTab{ NTType: 54, Index: 102, NumSymbols: 1, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return ast.NewCharConst(X[0]) }, }, @@ -1057,7 +1057,7 @@ var productionsTable = ProdTab{ NTType: 55, Index: 103, NumSymbols: 1, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return X[0], nil }, }, @@ -1067,7 +1067,7 @@ var productionsTable = ProdTab{ NTType: 55, Index: 104, NumSymbols: 1, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return X[0], nil }, }, @@ -1077,7 +1077,7 @@ var productionsTable = ProdTab{ NTType: 55, Index: 105, NumSymbols: 1, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return X[0], nil }, }, @@ -1087,7 +1087,7 @@ var productionsTable = ProdTab{ NTType: 56, Index: 106, NumSymbols: 1, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return X[0], nil }, }, @@ -1097,7 +1097,7 @@ var productionsTable = ProdTab{ NTType: 56, Index: 107, NumSymbols: 1, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return X[0], nil }, }, @@ -1107,7 +1107,7 @@ var productionsTable = ProdTab{ NTType: 56, Index: 108, NumSymbols: 1, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return X[0], nil }, }, @@ -1117,7 +1117,7 @@ var productionsTable = ProdTab{ NTType: 56, Index: 109, NumSymbols: 1, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return X[0], nil }, }, @@ -1127,7 +1127,7 @@ var productionsTable = ProdTab{ NTType: 56, Index: 110, NumSymbols: 1, - ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) { + ReduceFunc: func(X []Attrib) (Attrib, error) { return X[0], nil }, }, diff --git a/gocc/token/token.go b/gocc/token/token.go index 8697181..34276a2 100644 --- a/gocc/token/token.go +++ b/gocc/token/token.go @@ -3,7 +3,6 @@ package token import ( - "bytes" "fmt" "strconv" "unicode/utf8" @@ -23,20 +22,13 @@ const ( ) type Pos struct { - Offset int - Line int - Column int - Context Context + Offset int + Line int + Column int } func (p Pos) String() string { - // If the context provides a filename, provide a human-readable File:Line:Column representation. - switch src := p.Context.(type) { - case Sourcer: - return fmt.Sprintf("%s:%d:%d", src.Source(), p.Line, p.Column) - default: - return fmt.Sprintf("Pos(offset=%d, line=%d, column=%d)", p.Offset, p.Line, p.Column) - } + return fmt.Sprintf("Pos(offset=%d, line=%d, column=%d)", p.Offset, p.Line, p.Column) } type TokenMap struct { @@ -66,16 +58,6 @@ func (m TokenMap) StringType(typ Type) string { return fmt.Sprintf("%s(%d)", m.Id(typ), typ) } -// Equals returns returns true if the token Type and Lit are matches. -func (t *Token) Equals(rhs interface{}) bool { - switch rhsT := rhs.(type) { - case *Token: - return t == rhsT || (t.Type == rhsT.Type && bytes.Equal(t.Lit, rhsT.Lit)) - default: - return false - } -} - // CharLiteralValue returns the string value of the char literal. func (t *Token) CharLiteralValue() string { return string(t.Lit[1 : len(t.Lit)-1]) diff --git a/gocc/util/litconv.go b/gocc/util/litconv.go index 16407a4..1aeb616 100644 --- a/gocc/util/litconv.go +++ b/gocc/util/litconv.go @@ -9,9 +9,11 @@ import ( "unicode/utf8" ) -// Interface. +/* Interface */ -// RuneValue will convert the literal value of a scanned token to a rune. +/* +Convert the literal value of a scanned token to rune +*/ func RuneValue(lit []byte) rune { if lit[1] == '\\' { return escapeCharVal(lit) @@ -23,17 +25,22 @@ func RuneValue(lit []byte) rune { return r } -// UintValue will attempt to parse a byte-slice as a signed base-10 64-bit integer. +/* +Convert the literal value of a scanned token to int64 +*/ func IntValue(lit []byte) (int64, error) { return strconv.ParseInt(string(lit), 10, 64) } -// UintValue will attempt to parse a byte-slice as an unsigned base-10 64-bit integer. +/* +Convert the literal value of a scanned token to uint64 +*/ func UintValue(lit []byte) (uint64, error) { return strconv.ParseUint(string(lit), 10, 64) } -// Helpers. +/* Util */ + func escapeCharVal(lit []byte) rune { var i, base, max uint32 offset := 2 diff --git a/memory/vmemory.go b/memory/vmemory.go index c135b6e..23a683c 100644 --- a/memory/vmemory.go +++ b/memory/vmemory.go @@ -166,7 +166,7 @@ func (vm *VirtualMemory) NextGlobal(t types.CoreType) (Address, error) { @param t type of global variable returns next available address for global temporal **/ -func (vm *VirtualMemory) NextGlobalTemp(t types.CoreType) (Address, error) { +func (vm *VirtualMemory) NextGlobalTemp(t types.CoreType, space int) (Address, error) { switch t { case 0: // integer constant if vm.global_temp_int_count >= segmentSize { @@ -174,7 +174,7 @@ func (vm *VirtualMemory) NextGlobalTemp(t types.CoreType) (Address, error) { } addr := vm.global_temp_int_count + TempIntOffset + GlobalContext // increment counter - vm.global_temp_int_count++ + vm.global_temp_int_count+= space return Address(addr), nil case 1: // float constant @@ -183,7 +183,7 @@ func (vm *VirtualMemory) NextGlobalTemp(t types.CoreType) (Address, error) { } addr := vm.global_temp_float_count + TempFloatOffset + GlobalContext // increment counter - vm.global_temp_float_count++ + vm.global_temp_float_count+= space return Address(addr), nil case 2: // chars @@ -191,7 +191,7 @@ func (vm *VirtualMemory) NextGlobalTemp(t types.CoreType) (Address, error) { return Address(-1), errors.New("Too many temporal chars in local memory.") } addr := vm.global_temp_char_count + TempCharOffset + GlobalContext - vm.global_temp_char_count++ + vm.global_temp_char_count+= space return Address(addr), nil case 3: // booleans diff --git a/tests/array_test_1.isa b/tests/array_test_1.isa new file mode 100644 index 0000000..6c4d532 --- /dev/null +++ b/tests/array_test_1.isa @@ -0,0 +1,7 @@ +Program unitFuncTest : + variables{ + int a[10]; + } + main () { + output(1); + } \ No newline at end of file diff --git a/tests/encoding.obj b/tests/encoding.obj new file mode 100644 index 0000000..ee835cb Binary files /dev/null and b/tests/encoding.obj differ diff --git a/tests/parse_test.go b/tests/parse_test.go index 70e4e48..29a3bea 100644 --- a/tests/parse_test.go +++ b/tests/parse_test.go @@ -39,7 +39,7 @@ func readFile(path string) ([]byte, error) { func TestDuck(t *testing.T) { p := parser.NewParser() tests := []string{ - "call_test_3.isa", + "array_test_1.isa", } for _, test := range tests {