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 ( | ||||
| 	"fmt" | ||||
|  | @ -6,6 +6,8 @@ import ( | |||
| 	"strconv" | ||||
| 	"strings" | ||||
| 	"unicode/utf8" | ||||
| 
 | ||||
| 	"git.bonsai.cool/kayprish/pj1/pj1-go/util" | ||||
| ) | ||||
| 
 | ||||
| type TokenType int | ||||
|  | @ -102,7 +104,7 @@ func (t Token) String() string { | |||
| 
 | ||||
| type Lexer struct { | ||||
| 	source string | ||||
| 	tokens []Token | ||||
| 	Tokens []Token | ||||
| 
 | ||||
| 	startByte   int | ||||
| 	currentByte int | ||||
|  | @ -123,7 +125,7 @@ func (l *Lexer) ScanTokens() { | |||
| 		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 { | ||||
|  | @ -226,7 +228,7 @@ func (l *Lexer) scanToken() { | |||
| 		} else { | ||||
| 			// TODO: if there are multiple bad characters | ||||
| 			// 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': | ||||
| 				c = '\x1b' | ||||
| 			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 | ||||
| 			} | ||||
| 		} | ||||
|  | @ -360,7 +362,7 @@ func (l *Lexer) str() { | |||
| 	} | ||||
| 
 | ||||
| 	if l.atEnd() { | ||||
| 		error(l.line, "Unterminated string.") | ||||
| 		util.Error(l.line, "Unterminated string.") | ||||
| 		return | ||||
| 	} | ||||
| 
 | ||||
|  | @ -378,5 +380,5 @@ func (l *Lexer) addSimpleToken(ttype TokenType) { | |||
| 
 | ||||
| func (l *Lexer) addToken(ttype TokenType, literal interface{}) { | ||||
| 	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. | ||||
| 
 | ||||
| package main | ||||
| package lexer | ||||
| 
 | ||||
| import "strconv" | ||||
| 
 | ||||
|  | @ -5,10 +5,9 @@ import ( | |||
| 	"fmt" | ||||
| 	"io/ioutil" | ||||
| 	"os" | ||||
| ) | ||||
| 
 | ||||
| var ( | ||||
| 	hadError bool = false | ||||
| 	"git.bonsai.cool/kayprish/pj1/pj1-go/lexer" | ||||
| 	"git.bonsai.cool/kayprish/pj1/pj1-go/util" | ||||
| ) | ||||
| 
 | ||||
| func main() { | ||||
|  | @ -30,7 +29,7 @@ func runFile(path string) { | |||
| 	} | ||||
| 	run(string(bytes[:])) | ||||
| 
 | ||||
| 	if hadError { | ||||
| 	if util.HadError { | ||||
| 		os.Exit(65) | ||||
| 	} | ||||
| } | ||||
|  | @ -46,7 +45,7 @@ func runPrompt() { | |||
| 		fmt.Println(line) | ||||
| 		run(line) | ||||
| 
 | ||||
| 		hadError = false | ||||
| 		util.HadError = false | ||||
| 	} | ||||
| 	if err := scanner.Err(); err != nil { | ||||
| 		fmt.Fprintln(os.Stderr, "reading standard input:", err) | ||||
|  | @ -54,21 +53,11 @@ func runPrompt() { | |||
| } | ||||
| 
 | ||||
| func run(source string) { | ||||
| 	lexer := NewLexer(source) | ||||
| 	lexer.ScanTokens() | ||||
| 	var tokens []Token = lexer.tokens | ||||
| 	l := lexer.NewLexer(source) | ||||
| 	l.ScanTokens() | ||||
| 	var tokens []lexer.Token = l.Tokens | ||||
| 
 | ||||
| 	for _, token := range tokens { | ||||
| 		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š