128 lines
2.9 KiB
Go
128 lines
2.9 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"regexp"
|
|
"strings"
|
|
)
|
|
|
|
|
|
type ukaz struct {
|
|
opcode int
|
|
st_operandov int
|
|
format int // F1 - 1, F2 - 2, F3 - 3, F4 - 4, SIC - 5
|
|
velikost int //v bajtih
|
|
}
|
|
|
|
var ukazna_tabela = map[string]int{
|
|
"ADD": 0x18,
|
|
"ADDF": 0x58,
|
|
"ADDR": 0x90,
|
|
"AND": 0x40,
|
|
"CLEAR": 0xB4,
|
|
"COMP": 0x28,
|
|
"COMPF": 0x88,
|
|
"COMPR": 0xA0,
|
|
"DIV": 0x24,
|
|
"DIVF": 0x64,
|
|
"DIVR": 0x9C,
|
|
"FIX": 0xC4,
|
|
"FLOAT": 0xC0,
|
|
"HIO": 0xF4,
|
|
"J": 0x3C,
|
|
"JEQ": 0x30,
|
|
"JGT": 0x34,
|
|
"JLT": 0x38,
|
|
"JSUB": 0x48,
|
|
"LDA": 0x00,
|
|
"LDB": 0x68,
|
|
"LDCH": 0x50,
|
|
"LDF": 0x70,
|
|
"LDL": 0x08,
|
|
"LDS": 0x6C,
|
|
"LDT": 0x74,
|
|
"LDX": 0x04,
|
|
"LPS": 0xD0,
|
|
"MUL": 0x20,
|
|
"MULF": 0x60,
|
|
"MULR": 0x98,
|
|
"NORM": 0xC8,
|
|
"OR": 0x44,
|
|
"RD": 0xD4,
|
|
"RMO": 0xAC,
|
|
"RSUB": 0x4C,
|
|
"SHIFTL": 0xA4,
|
|
"SHIFTR": 0xA8,
|
|
"SIO": 0xF0,
|
|
"SSK": 0xEC,
|
|
"STA": 0x0C,
|
|
"STB": 0x78,
|
|
"STCH": 0x54,
|
|
"STF": 0x80,
|
|
"STI": 0xD4,
|
|
"STL": 0x14,
|
|
"STS": 0x74,
|
|
"STSW": 0xE8,
|
|
"STT": 0x84,
|
|
"STX": 0x10,
|
|
"SUB": 0x1C,
|
|
"SUBF": 0x5C,
|
|
"SUBR": 0x94,
|
|
"SVC": 0xB0,
|
|
"TD": 0xE0,
|
|
"TIO": 0xF8,
|
|
"TIX": 0x2C,
|
|
"TIXR": 0xB8,
|
|
"WD": 0xDC
|
|
}
|
|
|
|
// Pregledovalnik bere vhodne vrstice in jih razgradi na posamezne enote.
|
|
// Razpoznane elemente okvalificira (oznaka / mnemonik / operand / komentar)
|
|
// //<zbirniski stavek> ::== [<oznaka stavka>] <ločilo> <mnemonik> <ločilo> {<operand> <ločilo>} [<komentar>]
|
|
|
|
|
|
|
|
func main() {
|
|
//register, _ := regexp.Compile("A|X|L|B|S|T|F")
|
|
|
|
inputbyte, err := os.ReadFile("input_invalid.asm")
|
|
if err != nil {
|
|
fmt.Println("Error reading input file: ", err)
|
|
return
|
|
}
|
|
input := string(inputbyte)
|
|
pattern := regexp.MustCompile(`.*\n`)
|
|
matches := pattern.FindAllString(input, -1)
|
|
//var AST []ukaz
|
|
for _, el := range matches {
|
|
match_pure_comment, err := regexp.MatchString(`(?m)^[\t ]*\..*$\n`, el)
|
|
if err != nil {
|
|
fmt.Println("Error while matching string.")
|
|
}
|
|
if match_pure_comment {
|
|
continue
|
|
}
|
|
match_empty, err := regexp.MatchString(`(?m)^[\t ]*$\n`, el)
|
|
if err != nil {
|
|
fmt.Println("Error while matching string.")
|
|
}
|
|
if match_empty {
|
|
continue
|
|
}
|
|
match_F1, err := regexp.MatchString(`(?m)^[A-Za-z_]*[\t ]+[A-Za-z]+[\t ]*(\..*)?$\n`, el)
|
|
if err != nil {
|
|
fmt.Println("Error while matching string.")
|
|
}
|
|
if match_F1 {
|
|
var nov_ukaz ukaz
|
|
nov_ukaz.format = 1
|
|
nov_ukaz.st_operandov = 0
|
|
nov_ukaz.velikost = 1
|
|
pattern := regexp.MustCompile(`[\t ]+[A-Za-z]+`)
|
|
mnemonik := strings.Trim(re.FindString(el), " \n")
|
|
nov_ukaz.opcode = ukazna_tabela[mnemonik];
|
|
fmt.Print(mnemonik)
|
|
}
|
|
}
|
|
}
|