Move lexer and Error into separate packages
This commit is contained in:
		
							parent
							
								
									c102b47e9a
								
							
						
					
					
						commit
						e4733b20a6
					
				
					 4 changed files with 36 additions and 26 deletions
				
			
		|  | @ -1,4 +1,4 @@ | ||||||
| package main | package lexer | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
| 	"fmt" | 	"fmt" | ||||||
|  | @ -6,6 +6,8 @@ import ( | ||||||
| 	"strconv" | 	"strconv" | ||||||
| 	"strings" | 	"strings" | ||||||
| 	"unicode/utf8" | 	"unicode/utf8" | ||||||
|  | 
 | ||||||
|  | 	"git.bonsai.cool/kayprish/pj1/pj1-go/util" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| type TokenType int | type TokenType int | ||||||
|  | @ -102,7 +104,7 @@ func (t Token) String() string { | ||||||
| 
 | 
 | ||||||
| type Lexer struct { | type Lexer struct { | ||||||
| 	source string | 	source string | ||||||
| 	tokens []Token | 	Tokens []Token | ||||||
| 
 | 
 | ||||||
| 	startByte   int | 	startByte   int | ||||||
| 	currentByte int | 	currentByte int | ||||||
|  | @ -123,7 +125,7 @@ func (l *Lexer) ScanTokens() { | ||||||
| 		l.scanToken() | 		l.scanToken() | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	l.tokens = append(l.tokens, NewToken(EOF, "", nil, l.line)) | 	l.Tokens = append(l.Tokens, NewToken(EOF, "", nil, l.line)) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func (l Lexer) atEnd() bool { | func (l Lexer) atEnd() bool { | ||||||
|  | @ -226,7 +228,7 @@ func (l *Lexer) scanToken() { | ||||||
| 		} else { | 		} else { | ||||||
| 			// TODO: if there are multiple bad characters | 			// TODO: if there are multiple bad characters | ||||||
| 			// coalesce similar errors into one | 			// coalesce similar errors into one | ||||||
| 			error(l.line, fmt.Sprintf("Unexpected character, %v.", c)) | 			util.Error(l.line, fmt.Sprintf("Unexpected character, %v.", c)) | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  | @ -351,7 +353,7 @@ func (l *Lexer) str() { | ||||||
| 			case 'e': | 			case 'e': | ||||||
| 				c = '\x1b' | 				c = '\x1b' | ||||||
| 			default: | 			default: | ||||||
| 				error(l.line, fmt.Sprintf("Invalid escape sequence \\%v.", l.peek())) | 				util.Error(l.line, fmt.Sprintf("Invalid escape sequence \\%v.", l.peek())) | ||||||
| 				return | 				return | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
|  | @ -360,7 +362,7 @@ func (l *Lexer) str() { | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	if l.atEnd() { | 	if l.atEnd() { | ||||||
| 		error(l.line, "Unterminated string.") | 		util.Error(l.line, "Unterminated string.") | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | @ -378,5 +380,5 @@ func (l *Lexer) addSimpleToken(ttype TokenType) { | ||||||
| 
 | 
 | ||||||
| func (l *Lexer) addToken(ttype TokenType, literal interface{}) { | func (l *Lexer) addToken(ttype TokenType, literal interface{}) { | ||||||
| 	text := l.source[l.startByte:l.currentByte] | 	text := l.source[l.startByte:l.currentByte] | ||||||
| 	l.tokens = append(l.tokens, NewToken(ttype, text, literal, l.line)) | 	l.Tokens = append(l.Tokens, NewToken(ttype, text, literal, l.line)) | ||||||
| } | } | ||||||
|  | @ -1,6 +1,6 @@ | ||||||
| // Code generated by "stringer -type=TokenType"; DO NOT EDIT. | // Code generated by "stringer -type=TokenType"; DO NOT EDIT. | ||||||
| 
 | 
 | ||||||
| package main | package lexer | ||||||
| 
 | 
 | ||||||
| import "strconv" | import "strconv" | ||||||
| 
 | 
 | ||||||
|  | @ -5,10 +5,9 @@ import ( | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"io/ioutil" | 	"io/ioutil" | ||||||
| 	"os" | 	"os" | ||||||
| ) |  | ||||||
| 
 | 
 | ||||||
| var ( | 	"git.bonsai.cool/kayprish/pj1/pj1-go/lexer" | ||||||
| 	hadError bool = false | 	"git.bonsai.cool/kayprish/pj1/pj1-go/util" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| func main() { | func main() { | ||||||
|  | @ -30,7 +29,7 @@ func runFile(path string) { | ||||||
| 	} | 	} | ||||||
| 	run(string(bytes[:])) | 	run(string(bytes[:])) | ||||||
| 
 | 
 | ||||||
| 	if hadError { | 	if util.HadError { | ||||||
| 		os.Exit(65) | 		os.Exit(65) | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  | @ -46,7 +45,7 @@ func runPrompt() { | ||||||
| 		fmt.Println(line) | 		fmt.Println(line) | ||||||
| 		run(line) | 		run(line) | ||||||
| 
 | 
 | ||||||
| 		hadError = false | 		util.HadError = false | ||||||
| 	} | 	} | ||||||
| 	if err := scanner.Err(); err != nil { | 	if err := scanner.Err(); err != nil { | ||||||
| 		fmt.Fprintln(os.Stderr, "reading standard input:", err) | 		fmt.Fprintln(os.Stderr, "reading standard input:", err) | ||||||
|  | @ -54,21 +53,11 @@ func runPrompt() { | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func run(source string) { | func run(source string) { | ||||||
| 	lexer := NewLexer(source) | 	l := lexer.NewLexer(source) | ||||||
| 	lexer.ScanTokens() | 	l.ScanTokens() | ||||||
| 	var tokens []Token = lexer.tokens | 	var tokens []lexer.Token = l.Tokens | ||||||
| 
 | 
 | ||||||
| 	for _, token := range tokens { | 	for _, token := range tokens { | ||||||
| 		fmt.Println(token) | 		fmt.Println(token) | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| 
 |  | ||||||
| // TODO: might have to rename |  | ||||||
| func error(line int, msg string) { |  | ||||||
| 	report(line, "", msg) |  | ||||||
| 	hadError = true |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| func report(line int, where string, msg string) { |  | ||||||
| 	fmt.Fprintln(os.Stderr, "[line "+fmt.Sprint(line)+"] Error"+where+": "+msg) |  | ||||||
| } |  | ||||||
|  |  | ||||||
							
								
								
									
										19
									
								
								pj1-go/util/error.go
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								pj1-go/util/error.go
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,19 @@ | ||||||
|  | package util | ||||||
|  | 
 | ||||||
|  | import ( | ||||||
|  | 	"fmt" | ||||||
|  | 	"os" | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | var ( | ||||||
|  | 	HadError bool = false | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | func Error(line int, msg string) { | ||||||
|  | 	report(line, "", msg) | ||||||
|  | 	HadError = true | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func report(line int, where string, msg string) { | ||||||
|  | 	fmt.Fprintln(os.Stderr, "[line "+fmt.Sprint(line)+"] Error"+where+": "+msg) | ||||||
|  | } | ||||||
		Loading…
	
	Add table
		
		Reference in a new issue
	
	 Petar Kapriš
						Petar Kapriš