aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2015-03-23 00:39:14 +0100
committerDimitri Sokolyuk <demon@dim13.org>2015-03-23 00:39:14 +0100
commitdd5a92f37650da584f376746fa3ed867c01035d1 (patch)
treed605879c64c3c9ea93f066c3f953043cc1e0510f
parent2da427d49c7c04b83f8ad81534db471d3037a41a (diff)
Boiler plate
-rw-r--r--lexer.go20
-rw-r--r--parser.y16
-rw-r--r--tokens.go4
3 files changed, 35 insertions, 5 deletions
diff --git a/lexer.go b/lexer.go
index 8082e35..ec56ccc 100644
--- a/lexer.go
+++ b/lexer.go
@@ -2,6 +2,8 @@ package main
import (
"log"
+ "strconv"
+ "strings"
"unicode/utf8"
)
@@ -24,6 +26,24 @@ func (y *yyLex) Error(s string) {
func (y *yyLex) Lex(lval *yySymType) (ret int) {
item := <-y.items
+ switch item.typ {
+ case STRING:
+ lval.sval = item.val
+ case QUOTED:
+ lval.sval = strings.Replace(item.val, "''", "'", -1)
+ case INTEGER:
+ item.val = strings.Replace(item.val, "¯", "-", -1)
+ lval.ival, _ = strconv.Atoi(item.val)
+ case FLOAT:
+ item.val = strings.Replace(item.val, "¯", "-", -1)
+ lval.fval, _ = strconv.ParseFloat(item.val, 64)
+ case COMPLEX:
+ item.val = strings.Replace(item.val, "¯", "-", -1)
+ pos := strings.IndexAny(item.val, "jJ")
+ r, _ := strconv.ParseFloat(item.val[:pos], 64)
+ i, _ := strconv.ParseFloat(item.val[pos+1:], 64)
+ lval.cval = complex(r, i)
+ }
lval.sval = item.val
return item.typ
}
diff --git a/parser.y b/parser.y
index 4a4a5a3..6b88e03 100644
--- a/parser.y
+++ b/parser.y
@@ -1,13 +1,27 @@
%{
package main
+
+import "fmt"
%}
%union {
sval string
+ ival int
+ fval float64
+ cval complex128
}
+%token <sval> STRING
+%token <ival> INTEGER
+%token <fval> FLOAT
+%token <cval> COMPLEX
+
%%
-line:
+line
+ : STRING { fmt.Println($1) }
+ | INTEGER { fmt.Println($1) }
+ | FLOAT { fmt.Println($1) }
+ | COMPLEX { fmt.Println($1) }
%%
diff --git a/tokens.go b/tokens.go
index 5e43188..5c6f8d9 100644
--- a/tokens.go
+++ b/tokens.go
@@ -3,12 +3,8 @@ package main
const (
EOF = iota
BLANK
- INTEGER
- FLOAT
- COMPLEX
DIGIT
CHAR
- STRING
QUOTED
QUOTE
DOT