Some early lexer ideas
This commit is contained in:
		
							
								
								
									
										51
									
								
								mmql/definitions.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										51
									
								
								mmql/definitions.go
									
									
									
									
									
										Normal 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
									
								
							
							
						
						
									
										14
									
								
								mmql/lexer.go
									
									
									
									
									
										Normal 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
									
								
							
							
						
						
									
										17
									
								
								mmql/lexer_test.go
									
									
									
									
									
										Normal 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
									
								
							
							
						
						
									
										8
									
								
								mmql/statements.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,8 @@
 | 
			
		||||
package mmql
 | 
			
		||||
 | 
			
		||||
type IStatement interface {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type ActionStatement struct {
 | 
			
		||||
    IStatement
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										30
									
								
								mmql/stmt_buy.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								mmql/stmt_buy.go
									
									
									
									
									
										Normal 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 {
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
*/
 | 
			
		||||
		Reference in New Issue
	
	Block a user