Some early lexer ideas

This commit is contained in:
斟酌 鵬兄 2022-10-20 18:04:28 +08:00
parent 0acbda9a7a
commit c9ecbdde08
7 changed files with 131 additions and 0 deletions

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module github.com/tgckpg/mmqlengine
go 1.19

8
main.go Normal file
View File

@ -0,0 +1,8 @@
package main
import (
)
func main() {
}

51
mmql/definitions.go Normal file
View File

@ -0,0 +1,51 @@
package mmql
type TokenType int
type Token struct {
Type TokenType
Value string
}
const (
K_SPACES = "\t\n\r "
K_BRACKETS = "()[]{}"
K_QUOTES = "'\"`"
)
const (
UnknownToken TokenType = iota
KeywordToken
BracketToken
SquareBracketToken
CurlyBracketToken
SingleQuote
DoubleQuote
AccuteAccentQuote
)
var KEYWORDS = []string {
"ANYWHERE",
"BOUGHT",
"BUY",
"CREATE",
"EVERY",
"EXCHANGE",
"FOR",
"FROM",
"GET",
"LIMIT",
"LIST",
"OF",
"SELL",
"SOLD",
"TRIGGER",
"TRIGGERS",
"TYPE",
"UPDATE",
"WITH",
}
var STATEMENTS = map[string] func() ( IStatement, error ) {
"BUY": _buyStatement,
}

14
mmql/lexer.go Normal file
View File

@ -0,0 +1,14 @@
package mmql
import "fmt"
type Lexer struct {
expect TokenType
}
func ( this *Lexer ) Parse( line string ) {
for i, r := range line {
fmt.Printf( "%d: %s\n", i, string( r ) )
}
}

17
mmql/lexer_test.go Normal file
View File

@ -0,0 +1,17 @@
package mmql
import "testing"
func TestParse( t *testing.T ) {
lexer := Lexer{}
lexer.Parse( `
BUY 1 SHARES OF BTC_ETF
FOR 10 USD
FROM "MyBrokerAccount"
WITH LIMIT OF PURCHASING_POWER( "MyBrokerAccount", "QQQ" )
FOR EVERY
1 BTC OF USD_BTC SOLD
FROM "CoinBase"
` )
}

8
mmql/statements.go Normal file
View File

@ -0,0 +1,8 @@
package mmql
type IStatement interface {
}
type ActionStatement struct {
IStatement
}

30
mmql/stmt_buy.go Normal file
View File

@ -0,0 +1,30 @@
package mmql
/*
EXPECT: BUY
THEN EXPECT: [AmountType] OF [ProductType]
THEN EXPECT: FOR [AmountType]
THEN EXPECT: FROM [ExchangeType]
THEN OPT EXPECT: [ExchangeType]
THEN OPT EXPECT: [AND|OR]
THEN EXPECT: [ActionExpression]
THEN OPT EXPECT: FOR EVERY
THEN EXPECT: [AmountType] OF [ProductType]
THEN EXPECT: [SOLD|BOUGHT] FROM [ExchangeType]
*/
func _buyStatement() ( IStatement, error ) {
a := ActionStatement{}
var b IStatement = a
return b, nil
}
/*
func _buyStatement( lexer *Lexer ) ( ActionStatement, error ) {
token := lexer.GetToken()
if token.Type == KeywordToken && token.Value == "BUY" {
} else {
}
}
*/