Beispiel #1
0
Datei: lex.go Projekt: Xslxy/e8vm
func lexAsm8(x *lex8.Lexer) *lex8.Token {
	r := x.Rune()
	if x.IsWhite(r) {
		panic("incorrect token start")
	}

	switch r {
	case '\n':
		x.Next()
		return x.MakeToken(Endl)
	case '{':
		x.Next()
		return x.MakeToken(Lbrace)
	case '}':
		x.Next()
		return x.MakeToken(Rbrace)
	case '/':
		x.Next()
		return lex8.LexComment(x)
	case '"':
		return lex8.LexString(x, String, '"')
	}

	if isOperandChar(r) {
		return lexOperand(x)
	}

	x.Errorf("illegal char %q", r)
	x.Next()
	return x.MakeToken(lex8.Illegal)
}
Beispiel #2
0
func lexOperator(x *lex8.Lexer, r rune) *lex8.Token {
	switch r {
	case ';':
		return x.MakeToken(Semi)
	case '{', '}', '(', ')', '[', ']', ',':
		/* do nothing */
	case '/':
		r2 := x.Rune()
		if r2 == '/' || r2 == '*' {
			return lex8.LexComment(x)
		} else if r2 == '=' {
			x.Next()
		}
	case '+', '-', '&', '|':
		r2 := x.Rune()
		if r2 == r || r2 == '=' {
			x.Next()
		}
	case '*', '%', '^', '=', '!', ':':
		r2 := x.Rune()
		if r2 == '=' {
			x.Next()
		}
	case '.':
		r2 := x.Rune()
		if r2 == '.' {
			x.Next()
			r3 := x.Rune()
			if r3 != '.' {
				x.Errorf("expect ..., but see ..")
				return x.MakeToken(Operator)
			}
			x.Next()
		}
	case '>', '<':
		r2 := x.Rune()
		if r2 == r {
			x.Next()
			r3 := x.Rune()
			if r3 == '=' {
				x.Next()
			}
		} else if r2 == '=' {
			x.Next()
		}
	default:
		return nil
	}

	return x.MakeToken(Operator)
}