2016-07-10 14:35:33 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-10-19 20:20:43 +02:00
|
|
|
"bufio"
|
2016-07-10 14:35:33 +02:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2016-10-19 20:20:43 +02:00
|
|
|
"io"
|
2016-07-10 14:35:33 +02:00
|
|
|
"strconv"
|
2016-10-19 20:20:43 +02:00
|
|
|
"strings"
|
2016-07-10 14:35:33 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2016-10-19 20:20:43 +02:00
|
|
|
WORD = iota // [A-Za-z_-]+
|
|
|
|
NUMBER // [0-9]+
|
|
|
|
NEWLINE // \n
|
|
|
|
ERROR // Error
|
2016-07-10 14:35:33 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type location struct {
|
2016-10-19 20:20:43 +02:00
|
|
|
line int
|
2016-07-10 14:35:33 +02:00
|
|
|
column int
|
|
|
|
}
|
|
|
|
|
|
|
|
type token struct {
|
2016-10-19 20:20:43 +02:00
|
|
|
location location // Position of the token
|
|
|
|
value string // Text content of the token
|
|
|
|
kind int // Kind of the token
|
2016-07-10 14:35:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type tokenizer struct {
|
2016-10-19 20:20:43 +02:00
|
|
|
location location // Current position
|
|
|
|
value []byte // Current token string
|
|
|
|
reader *bufio.Reader // Reader
|
|
|
|
tokens chan<- token // Output token channel
|
2016-07-10 14:35:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
func isSpace(c byte) bool {
|
|
|
|
return c == ' ' || c == '\r' || c == '\t'
|
|
|
|
}
|
|
|
|
|
|
|
|
func isNumber(c byte) bool {
|
|
|
|
return c >= '0' && c <= '9'
|
|
|
|
}
|
|
|
|
|
|
|
|
func isWordHead(c byte) bool {
|
2016-10-19 20:20:43 +02:00
|
|
|
if c >= 'a' && c <= 'z' {
|
|
|
|
c -= 32
|
|
|
|
}
|
2016-07-10 14:35:33 +02:00
|
|
|
return c >= 'a' && c <= 'z' || c == '_'
|
|
|
|
}
|
|
|
|
|
|
|
|
func isWordTail(c byte) bool {
|
|
|
|
return isWordHead(c) || isNumber(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
2016-10-19 20:20:43 +02:00
|
|
|
func (t *tokenizer) send(start location, kind int) {
|
|
|
|
t.tokens <- token{start, string(t.value), kind}
|
2016-07-10 14:35:33 +02:00
|
|
|
t.value = []byte{}
|
|
|
|
}
|
|
|
|
|
2016-10-19 20:20:43 +02:00
|
|
|
// XXX: the handling could probably be simplified by extending the "byte"
|
|
|
|
// to also include a special value for io.EOF and other errors
|
2016-07-10 14:35:33 +02:00
|
|
|
func (t *tokenizer) peek() (byte, error) {
|
|
|
|
buf, err := t.reader.Peek(1)
|
|
|
|
return buf[0], err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tokenizer) eat() (byte, error) {
|
|
|
|
c, err := t.reader.ReadByte()
|
2016-10-19 20:20:43 +02:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2016-07-10 14:35:33 +02:00
|
|
|
|
|
|
|
if c == '\n' {
|
2016-10-19 20:20:43 +02:00
|
|
|
t.location.line++
|
|
|
|
t.location.column = 0
|
2016-07-10 14:35:33 +02:00
|
|
|
} else {
|
2016-10-19 20:20:43 +02:00
|
|
|
t.location.column++
|
2016-07-10 14:35:33 +02:00
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
func (t *tokenizer) step() error {
|
|
|
|
t.value = []byte{}
|
|
|
|
c, err := t.peek()
|
|
|
|
|
2016-10-19 20:20:43 +02:00
|
|
|
if err == io.EOF {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-07-10 14:35:33 +02:00
|
|
|
|
2016-10-19 20:20:43 +02:00
|
|
|
start := t.location
|
2016-07-10 14:35:33 +02:00
|
|
|
switch {
|
|
|
|
case isSpace(c):
|
2016-10-19 20:20:43 +02:00
|
|
|
c, err = t.eat()
|
2016-07-10 14:35:33 +02:00
|
|
|
case c == '\n':
|
|
|
|
c, err = t.eat()
|
|
|
|
t.value = append(t.value, c)
|
|
|
|
|
2016-10-19 20:20:43 +02:00
|
|
|
t.send(start, NEWLINE)
|
2016-07-10 14:35:33 +02:00
|
|
|
case isNumber(c):
|
|
|
|
c, err = t.eat()
|
|
|
|
t.value = append(t.value, c)
|
|
|
|
|
|
|
|
for {
|
|
|
|
c, err = t.peek()
|
2016-10-19 20:20:43 +02:00
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-07-10 14:35:33 +02:00
|
|
|
|
2016-10-19 20:20:43 +02:00
|
|
|
if !isNumber(c) {
|
|
|
|
break
|
|
|
|
}
|
2016-07-10 14:35:33 +02:00
|
|
|
|
2016-10-19 20:20:43 +02:00
|
|
|
c, err = t.eat()
|
2016-07-10 14:35:33 +02:00
|
|
|
t.value = append(t.value, c)
|
|
|
|
}
|
2016-10-19 20:20:43 +02:00
|
|
|
t.send(start, NUMBER)
|
2016-07-10 14:35:33 +02:00
|
|
|
case isWordHead(c):
|
|
|
|
c, err = t.eat()
|
|
|
|
t.value = append(t.value, c)
|
|
|
|
|
|
|
|
for {
|
|
|
|
c, err = t.peek()
|
2016-10-19 20:20:43 +02:00
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-07-10 14:35:33 +02:00
|
|
|
|
2016-10-19 20:20:43 +02:00
|
|
|
if !isWordTail(c) {
|
|
|
|
break
|
|
|
|
}
|
2016-07-10 14:35:33 +02:00
|
|
|
|
2016-10-19 20:20:43 +02:00
|
|
|
c, err = t.eat()
|
2016-07-10 14:35:33 +02:00
|
|
|
t.value = append(t.value, c)
|
|
|
|
}
|
2016-10-19 20:20:43 +02:00
|
|
|
t.send(start, WORD)
|
2016-07-10 14:35:33 +02:00
|
|
|
case c == '/':
|
|
|
|
c, err = t.eat()
|
|
|
|
t.value = append(t.value, c)
|
|
|
|
|
|
|
|
c, err = t.peek()
|
2016-10-19 20:20:43 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-07-10 14:35:33 +02:00
|
|
|
|
|
|
|
if c != '/' {
|
|
|
|
return errors.New("unrecognized input")
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
c, err = t.peek()
|
2016-10-19 20:20:43 +02:00
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-07-10 14:35:33 +02:00
|
|
|
|
2016-10-19 20:20:43 +02:00
|
|
|
if c == '\n' {
|
|
|
|
break
|
|
|
|
}
|
2016-07-10 14:35:33 +02:00
|
|
|
t.eat()
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return errors.New("unrecognized input")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func tokenize(r io.Reader, tokens chan<- token) {
|
|
|
|
t := tokenizer{
|
2016-10-19 20:20:43 +02:00
|
|
|
location: location{line: 1, column: 0},
|
|
|
|
tokens: tokens,
|
|
|
|
reader: bufio.NewReader(r),
|
2016-07-10 14:35:33 +02:00
|
|
|
}
|
|
|
|
for {
|
|
|
|
err := t.step()
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
2016-10-19 20:20:43 +02:00
|
|
|
t.tokens <- token{t.location, fmt.Sprintf("line %d, column %d: %s",
|
|
|
|
t.location.line, t.location.column, err.Error()), ERROR}
|
2016-07-10 14:35:33 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(tokens)
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
const (
|
|
|
|
IHALT = iota
|
|
|
|
IADD
|
|
|
|
ISUBTRACT
|
|
|
|
ISTORE
|
|
|
|
ILOAD
|
|
|
|
IBRANCH
|
|
|
|
IBRANCH_IF_ZERO
|
|
|
|
IBRANCH_IF_POSITIVE
|
|
|
|
IINPUT
|
|
|
|
IOUTUT
|
|
|
|
IDATA
|
|
|
|
)
|
|
|
|
|
2016-10-19 20:20:43 +02:00
|
|
|
var instructions = map[string]int{
|
2016-07-10 14:35:33 +02:00
|
|
|
"HLT": IHALT,
|
|
|
|
"COB": IHALT,
|
|
|
|
"ADD": IADD,
|
|
|
|
"SUB": ISUBTRACT,
|
|
|
|
"STA": ISTORE,
|
|
|
|
"LDA": ILOAD,
|
|
|
|
"BRA": IBRANCH,
|
|
|
|
"BRZ": IBRANCH_IF_ZERO,
|
|
|
|
"BRP": IBRANCH_IF_POSITIVE,
|
|
|
|
"INP": IINPUT,
|
|
|
|
"OUT": IOUTUT,
|
|
|
|
"DAT": IDATA,
|
|
|
|
}
|
|
|
|
|
|
|
|
type instruction struct {
|
2016-10-19 20:20:43 +02:00
|
|
|
id int
|
2016-07-10 14:35:33 +02:00
|
|
|
target string
|
|
|
|
number int
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
type assembler struct {
|
|
|
|
tokens chan token
|
|
|
|
output []instruction
|
|
|
|
labels map[string]int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *assembler) step() (bool, error) {
|
|
|
|
token, ok := <-a.tokens
|
2016-10-19 20:20:43 +02:00
|
|
|
if !ok {
|
|
|
|
return false, nil
|
|
|
|
}
|
2016-07-10 14:35:33 +02:00
|
|
|
|
|
|
|
// TODO: add token location information to returned errors
|
|
|
|
|
|
|
|
switch token.kind {
|
|
|
|
case WORD:
|
|
|
|
canonical := strings.ToUpper(token.value)
|
|
|
|
instr, found := instructions[canonical]
|
|
|
|
|
|
|
|
// Not found in the instruction list
|
|
|
|
// Assume it is a label
|
|
|
|
if !found {
|
|
|
|
if _, dup := a.labels[canonical]; dup {
|
|
|
|
return false, fmt.Errorf("Duplicate label: %s", canonical)
|
|
|
|
}
|
|
|
|
a.labels[canonical] = len(a.output)
|
|
|
|
|
|
|
|
token, ok = <-a.tokens
|
|
|
|
if !ok {
|
|
|
|
return false, errors.New("Unexpected end of file")
|
|
|
|
}
|
|
|
|
if token.kind != WORD {
|
|
|
|
return false, errors.New("Expected word")
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX: it might be better to classify this in the lexer
|
|
|
|
canonical = strings.ToUpper(token.value)
|
|
|
|
instr, found = instructions[canonical]
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
return false, fmt.Errorf("Unknown instruction: %s", canonical)
|
|
|
|
}
|
|
|
|
|
|
|
|
instrHolder := instruction{id: instr}
|
|
|
|
token, ok := <-a.tokens
|
|
|
|
if !ok {
|
|
|
|
// This is fine, just assume zero
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
switch token.kind {
|
|
|
|
case WORD:
|
|
|
|
instrHolder.target = strings.ToUpper(token.value)
|
|
|
|
case NEWLINE:
|
|
|
|
// This is fine, just assume zero
|
|
|
|
case NUMBER:
|
|
|
|
instrHolder.number, _ = strconv.Atoi(token.value)
|
|
|
|
case ERROR:
|
|
|
|
return false, errors.New(token.value)
|
|
|
|
}
|
|
|
|
a.output = append(a.output, instrHolder)
|
|
|
|
case NEWLINE:
|
|
|
|
// Ignore empty lines
|
|
|
|
case NUMBER:
|
|
|
|
return false, errors.New("Unexpected number")
|
|
|
|
case ERROR:
|
|
|
|
return false, errors.New(token.value)
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Assemble(r io.Reader) (code []int16, err error) {
|
|
|
|
a := assembler{tokens: make(chan token)}
|
|
|
|
go tokenize(r, a.tokens)
|
|
|
|
|
|
|
|
for {
|
|
|
|
cont, err := a.step()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !cont {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-19 20:20:43 +02:00
|
|
|
for _, x := range a.output {
|
2016-07-10 14:35:33 +02:00
|
|
|
n := x.id * 100
|
|
|
|
if len(x.target) != 0 {
|
|
|
|
if resolved, ok := a.labels[x.target]; !ok {
|
|
|
|
return nil, errors.New("Unknown label")
|
|
|
|
} else {
|
|
|
|
n += resolved
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
n += x.number
|
|
|
|
}
|
|
|
|
code = append(code, int16(n))
|
|
|
|
}
|
|
|
|
return code, nil
|
|
|
|
}
|