checkpoint

This commit is contained in:
aljazbrodar. 2026-01-15 15:46:38 +01:00
parent dadc137833
commit 3856635345
7 changed files with 241 additions and 35 deletions

View file

@ -78,7 +78,12 @@ func (f format_F2_n) Temp() {}
func (f format_F2_r) Temp() {}
func (f format_F2_rn) Temp() {}
func (f format_F2_rr) Temp() {}
func (f format_F3_m) Temp() {}
func (f format_F4_m) Temp() {}
func (f format_D) Temp() {}
func (f format_D_n) Temp() {}
func (f format_M_i) Temp() {}
func (f format_M_r) Temp() {}
var register string = `A|X|L|B|S|T|F`
@ -92,7 +97,7 @@ type format_F1 struct {
type format_F2_n struct {
oznaka string
opcode int
format int
format int // 2
velikost int
n string
}
@ -100,7 +105,7 @@ type format_F2_n struct {
type format_F2_r struct {
oznaka string
opcode int
format int
format int // 6
velikost int
r1 string
}
@ -108,7 +113,7 @@ type format_F2_r struct {
type format_F2_rn struct {
oznaka string
opcode int
format int
format int // 2
velikost int
r1 string
n string
@ -117,7 +122,7 @@ type format_F2_rn struct {
type format_F2_rr struct {
oznaka string
opcode int
format int
format int // 2
velikost int
r1 string
r2 string
@ -126,7 +131,7 @@ type format_F2_rr struct {
type format_F3_m struct {
oznaka string
opcode int
format int
format int // 3
velikost int
naslov string
x bool
@ -137,6 +142,47 @@ type format_F3_m struct {
i bool
}
type format_F4_m struct {
oznaka string
opcode int
format int // 4
velikost int
naslov string
x bool
b bool
p bool
e bool
n bool
i bool
}
type format_D struct {
oznaka string
format int // 6
velikost int
}
type format_D_n struct {
oznaka string
format int // 7
velikost int
operand string
}
type format_M_r struct {
oznaka string
format int // 8
velikost int
operand string
}
type format_M_i struct {
oznaka string
format int // 9
velikost int
operand string
}
func remove_comments_and_empty_lines(el string) bool {
match_pure_comment, err := regexp.MatchString(`(?m)^[\t ]*\..*$\n`, el)
if err != nil {
@ -166,7 +212,12 @@ func check_F1(el string, AST *[]ukaz) bool {
nov_ukaz.velikost = 1
pattern := regexp.MustCompile(`[\t ]+[A-Za-z]+`)
mnemonik := strings.Trim(pattern.FindString(el), "\t ")
nov_ukaz.opcode = ukazna_tabela[mnemonik];
val, ok := ukazna_tabela[mnemonik];
if !ok {
fmt.Println("Error: Opcode not found.")
return false
}
nov_ukaz.opcode = val
//fmt.Println("F1: ", mnemonik, nov_ukaz.opcode)
*AST = append(*AST, nov_ukaz)
return true
@ -220,7 +271,12 @@ func check_F2_r(el string, AST *[]ukaz) bool {
nov_ukaz.oznaka = ""
}
re_op := regexp.MustCompile(`[\t ]+[A-Za-z]+`)
nov_ukaz.opcode = ukazna_tabela[strings.Trim(re_op.FindString(el), "\t ")]
val, ok := ukazna_tabela[strings.Trim(re_op.FindString(el), "\t ")]
if !ok {
fmt.Println("Error: Opcode not found.")
return false
}
nov_ukaz.opcode = val
re_par := regexp.MustCompile(`[\t ]+[A-Za-z]+ (`+register+`)`)
nov_ukaz.r1 = strings.Split(strings.Trim(re_par.FindString(el), "\t \n"), " ")[1]
//fmt.Println(nov_ukaz)
@ -246,7 +302,12 @@ func check_F2_rn(el string, AST *[]ukaz) bool {
nov_ukaz.oznaka = ""
}
re_op := regexp.MustCompile(`[\t ]+[A-Za-z]+`)
nov_ukaz.opcode = ukazna_tabela[strings.Trim(re_op.FindString(el), "\t ")]
val, ok := ukazna_tabela[strings.Trim(re_op.FindString(el), "\t ")]
if !ok {
fmt.Println("Error: Opcode not found.")
return false
}
nov_ukaz.opcode = val
re_par1 := regexp.MustCompile(`[\t ]+[A-Za-z]+ (`+register+`)`)
nov_ukaz.r1 = strings.Split(strings.Trim(re_par1.FindString(el), "\t \n"), " ")[1]
re_par2 := regexp.MustCompile(`(`+register+`), ([0-9]+),?`)
@ -279,7 +340,12 @@ func check_F2_rr(el string, AST *[]ukaz) bool {
nov_ukaz.oznaka = ""
}
re_op := regexp.MustCompile(`[\t ]+[A-Za-z]+`)
nov_ukaz.opcode = ukazna_tabela[strings.Trim(re_op.FindString(el), "\t ")]
val, ok := ukazna_tabela[strings.Trim(re_op.FindString(el), "\t ")]
if !ok {
fmt.Println("Error: Opcode not found.")
return false
}
nov_ukaz.opcode = val
re_par1 := regexp.MustCompile(`[\t ]+[A-Za-z]+ (`+register+`)`)
nov_ukaz.r1 = strings.Split(strings.Trim(re_par1.FindString(el), "\t \n"), " ")[1]
re_par2 := regexp.MustCompile(`(`+register+`), (`+register+`)`)
@ -292,7 +358,128 @@ func check_F2_rr(el string, AST *[]ukaz) bool {
}
func check_F3_m(el string, AST *[]ukaz) bool {
match_F3_m, err := regexp.MatchString(`(?m)^[A-Za-z_]*[\t ]+[A-Za-z]+ ([A-Za-z]+|(#?|@?)[0-9+]+)(, X)?[\t ]*(\..*)?$\n`, el)
if err != nil {
fmt.Println("Error while matching string.")
}
if match_F3_m {
var nov_ukaz format_F3_m
nov_ukaz.format = 3
nov_ukaz.velikost = 3
if !(strings.HasPrefix(el, " ") || strings.HasPrefix(el, "\t")) {
re := regexp.MustCompile(`(?m)^[A-Za-z]+`)
nov_ukaz.oznaka = re.FindString(el)
} else {
nov_ukaz.oznaka = ""
}
re_op := regexp.MustCompile(`[\t ]+[A-Za-z]+`)
val, ok := ukazna_tabela[strings.Trim(re_op.FindString(el), "\t ")]
if !ok {
fmt.Println("Error: Opcode not found.")
return false
}
nov_ukaz.opcode = val
// naslov
re_par1 := regexp.MustCompile(`[\t ]+[A-Za-z]+ ([A-Za-z]+|(#?|@?)[0-9+]+)`)
nov_ukaz.naslov = strings.Split(strings.Trim(re_par1.FindString(el), "\t \n"), " ")[1]
if nov_ukaz.naslov[0] == '@' {
nov_ukaz.n = true
nov_ukaz.i = false
nov_ukaz.naslov = nov_ukaz.naslov[1:len(nov_ukaz.naslov)]
} else if nov_ukaz.naslov[0] == '#' {
nov_ukaz.n = false
nov_ukaz.i = true
nov_ukaz.naslov = nov_ukaz.naslov[1:len(nov_ukaz.naslov)]
} else {
//temp
nov_ukaz.n = false
nov_ukaz.i = false
}
// indeksno naslavljanje
re_x := regexp.MustCompile(`(, X)[\t ]*(\..*)?\n`)
if re_x.FindString(el) != "" {
nov_ukaz.x = true
} else {
nov_ukaz.x = false
}
nov_ukaz.e = false
//fmt.Println(el, nov_ukaz)
*AST = append(*AST, nov_ukaz)
return true
}
return false
}
func check_F4_m(el string, AST *[]ukaz) bool {
match_F4_m, err := regexp.MatchString(`(?m)^[A-Za-z_]*[\t ]+\+[A-Za-z]+ ([A-Za-z]+|(#?|@?)[0-9+]+)(, X)?[\t ]*(\..*)?$\n`, el)
if err != nil {
fmt.Println("Error while matching string.")
}
if match_F4_m {
var nov_ukaz format_F4_m
nov_ukaz.format = 4
nov_ukaz.velikost = 4
if !(strings.HasPrefix(el, " ") || strings.HasPrefix(el, "\t")) {
re := regexp.MustCompile(`(?m)^[A-Za-z]+`)
nov_ukaz.oznaka = re.FindString(el)
} else {
nov_ukaz.oznaka = ""
}
re_op := regexp.MustCompile(`[\t ]+\+[A-Za-z]+`)
// skip "+"
val, ok := ukazna_tabela[strings.Trim(re_op.FindString(el), "\t +")]
if !ok {
fmt.Println("Error: Opcode not found.")
return false
}
nov_ukaz.opcode = val
// naslov
re_par1 := regexp.MustCompile(`[\t ]+\+[A-Za-z]+ ([A-Za-z]+|(#?|@?)[0-9+]+)`)
nov_ukaz.naslov = strings.Split(strings.Trim(re_par1.FindString(el), "\t \n"), " ")[1]
if nov_ukaz.naslov[0] == '@' {
nov_ukaz.n = true
nov_ukaz.i = false
nov_ukaz.naslov = nov_ukaz.naslov[1:len(nov_ukaz.naslov)]
} else if nov_ukaz.naslov[0] == '#' {
nov_ukaz.n = false
nov_ukaz.i = true
nov_ukaz.naslov = nov_ukaz.naslov[1:len(nov_ukaz.naslov)]
} else {
nov_ukaz.n = false
nov_ukaz.i = false
}
// indeksno naslavljanje
re_x := regexp.MustCompile(`(, X)[\t ]*(\..*)?\n`)
if re_x.FindString(el) != "" {
nov_ukaz.x = true
} else {
nov_ukaz.x = false
}
nov_ukaz.e = true
fmt.Println(el, nov_ukaz)
*AST = append(*AST, nov_ukaz)
return true
}
return false
}
func check_D(el string, AST *[]ukaz) {
match_D, err := regexp.MatchString(`(?m)^[A-Za-z_]*[\t ]+NOBASE[\t ]*(\..*)?$\n`, el)
if err != nil {
fmt.Println("Error while matching string.")
}
if match_D {
var nova_direktiva format_D
if !(strings.HasPrefix(el, " ") || strings.HasPrefix(el, "\t")) {
re := regexp.MustCompile(`(?m)^[A-Za-z]+`)
nov_direktiva.oznaka = re.FindString(el)
} else {
nov_direktiva.oznaka = ""
}
*AST = append(*AST, nova_direktiva)
return true
}
return false
}
// Pregledovalnik bere vhodne vrstice in jih razgradi na posamezne enote.
@ -314,6 +501,9 @@ func main() {
if remove_comments_and_empty_lines(el) {
continue
}
if check_D(el, &AST) {
continue
}
if check_F1(el, &AST) {
continue
}
@ -329,8 +519,15 @@ func main() {
if check_F2_rr(el, &AST) {
continue
}
// !!! direktivo START je treba preveriti pred F3, sicer jo bo dalo sem
if check_F3_m(el, &AST) {
continue
}
if check_F4_m(el, &AST) {
continue
}
}
}