diff --git a/ass3/zbirnik/parsing/input_invalid.asm b/ass3/zbirnik/parsing/input_invalid.asm index c5a8c48..6888b19 100644 --- a/ass3/zbirnik/parsing/input_invalid.asm +++ b/ass3/zbirnik/parsing/input_invalid.asm @@ -1,8 +1,15 @@ arithr START 0 .komentar1 - +LDX #10 + LDX #10 SHIFTR A, 10, X - +LDS #11 .komentar2 + LDS #11 .komentar2 LDT #5 + NOBASE .koment + NOBASE + BASE ZERO .KOMENTAR + ORG tab-tab +LAB NOBASE + EQU +LAB NOBASE .koment .komentar3 LDA ZERO ADDR S, A @@ -11,9 +18,7 @@ arithr START 0 .komentar1 .komentar4 LDA ZERO - +LDA ZERO, X .test ADDR S, A - LDA 123, X . kom SUBR T, A STA diff @@ -22,7 +27,7 @@ arithr START 0 .komentar1 MULR T, A STA prod - +LDA ZERO + LDA ZERO ADDR S, A DIVR T, A STA quot diff --git a/ass3/zbirnik/parsing/pregledovalnik.go b/ass3/zbirnik/parsing/pregledovalnik.go index 224dd4f..a0ca3d7 100644 --- a/ass3/zbirnik/parsing/pregledovalnik.go +++ b/ass3/zbirnik/parsing/pregledovalnik.go @@ -158,12 +158,14 @@ type format_F4_m struct { type format_D struct { oznaka string + ime string //NOBASE format int // 6 velikost int } type format_D_n struct { oznaka string + ime string format int // 7 velikost int operand string @@ -171,6 +173,7 @@ type format_D_n struct { type format_M_r struct { oznaka string + ime string format int // 8 velikost int operand string @@ -178,11 +181,12 @@ type format_M_r struct { type format_M_i struct { oznaka string + ime 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 { @@ -456,32 +460,78 @@ func check_F4_m(el string, AST *[]ukaz) bool { nov_ukaz.x = false } nov_ukaz.e = true - fmt.Println(el, nov_ukaz) + //fmt.Println(el, nov_ukaz) *AST = append(*AST, nov_ukaz) return true } return false } -func check_D(el string, AST *[]ukaz) { +func check_D(el string, AST *[]ukaz) bool { 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 + nova_direktiva.ime = "NOBASE" + nova_direktiva.format = 6 + nova_direktiva.velikost = 0 if !(strings.HasPrefix(el, " ") || strings.HasPrefix(el, "\t")) { re := regexp.MustCompile(`(?m)^[A-Za-z]+`) - nov_direktiva.oznaka = re.FindString(el) + nova_direktiva.oznaka = re.FindString(el) } else { - nov_direktiva.oznaka = "" + nova_direktiva.oznaka = "" } *AST = append(*AST, nova_direktiva) + //fmt.Println(el, nova_direktiva) return true } return false } +func check_D_n(el string, AST *[]ukaz) bool { + match_D_n, err := regexp.MatchString(`(?m)^[A-Za-z]*[\t ]+(START|END|ORG|EQU|BASE) ([A-Za-z\+\-\*]+|[0-9\+\-\*]+)[\t ]*(\..*)?$\n`, el) + if err != nil { + fmt.Println("Error while matching string.") + } + if match_D_n { + var nova_direktiva format_D_n + re_ime := regexp.MustCompile(`START|END|ORG|EQU|BASE`) + nova_direktiva.ime = re_ime.FindString(el) + nova_direktiva.format = 7 + nova_direktiva.velikost = 0 + if !(strings.HasPrefix(el, " ") || strings.HasPrefix(el, "\t")) { + re := regexp.MustCompile(`(?m)^[A-Za-z]+`) + nova_direktiva.oznaka = re.FindString(el) + } else { + nova_direktiva.oznaka = "" + } + re_ope := regexp.MustCompile(`(START|END|ORG|EQU|BASE) ([A-Za-z\+\-\*]+|[0-9\+\-\*]+)`) + nova_direktiva.operand = strings.Split(re_ope.FindString(el), " ")[1] + *AST = append(*AST, nova_direktiva) + //fmt.Println(el, nova_direktiva) + return true + } + return false +} + +func check_M_r(el string, AST *[]ukaz) bool { + match_M_r, err := regexp.MatchString(`(?m)^[A-Za-z]*[\t ]+(RESW|RESB) ([0-9]+)[\t ]*(\..*)?$\n`, el) + if err != nil { + fmt.Println("Error while matching string.") + } + if match_M_r { + var nova_direktiva format_D_n + re_ime := regexp.MustCompile(`RESW|RESB`) + nova_direktiva.ime = re_ime.FindString(el) + nova_direktiva.format = 8 + //predpostavi celostevilski operand in doloci velikost + return true + } + return false +} + // Pregledovalnik bere vhodne vrstice in jih razgradi na posamezne enote. // Razpoznane elemente okvalificira (oznaka / mnemonik / operand / komentar) // // ::== [] { } [] @@ -504,6 +554,12 @@ func main() { if check_D(el, &AST) { continue } + if check_D_n(el, &AST) { + continue + } + if check_M_r(el, &AST) { + continue + } if check_F1(el, &AST) { continue } @@ -519,7 +575,6 @@ 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 } diff --git a/ass3/zbirnik/parsing/test.asm b/ass3/zbirnik/parsing/test.asm index 151229f..99d45f3 100644 --- a/ass3/zbirnik/parsing/test.asm +++ b/ass3/zbirnik/parsing/test.asm @@ -2,8 +2,8 @@ PRG START 300 LDA #bytes_len DIV #3 LDA bytes_len - LDB #tab - BASE #tab + LDB tab + BASE tab NOBASE HALT J HALT END PRG diff --git a/ass3/zbirnik/parsing/test.log b/ass3/zbirnik/parsing/test.log index e69de29..a68a4fa 100644 --- a/ass3/zbirnik/parsing/test.log +++ b/ass3/zbirnik/parsing/test.log @@ -0,0 +1,18 @@ +***** Section ***** +Stats: size=29 blocks=29 symbols=7 literals=0 relocations=0 +Blocks + name start size #ins #dir #sto + 0012C 0001D 5 10 1 +Symbols + name hex dec scope kind type description + HALT 000138 312 local relative code label + PRG 00012C 300 exported relative code label + bytes_len 000009 9 local absolute notlabel test-tab + tab 00013D 317 local relative data label + tab0 00013B 315 local relative data label + tab2 000146 326 local relative data label + test 000146 326 local absolute notlabel * +Literals + label definition +Relocations + address length flag symbol diff --git a/ass3/zbirnik/parsing/test.lst b/ass3/zbirnik/parsing/test.lst index e69de29..9f1e5a5 100644 --- a/ass3/zbirnik/parsing/test.lst +++ b/ass3/zbirnik/parsing/test.lst @@ -0,0 +1,17 @@ +0012C PRG START 300 +0012C 010009 LDA #bytes_len +0012F 250003 DIV #3 +00132 030009 LDA bytes_len +00135 6B2005 LDB tab +00135 BASE tab +00135 NOBASE +00138 3F2FFD HALT J HALT +0013B END PRG + +0013B 0000 tab0 RESB 2 +0013D 000001 tab WORD 1 +00140 000002 WORD 2 +00143 000003 WORD 3 +00146 test EQU * +00146 bytes_len EQU test-tab +00146 000001 tab2 WORD 1 diff --git a/ass3/zbirnik/parsing/test.obj b/ass3/zbirnik/parsing/test.obj index e69de29..7849bf4 100644 --- a/ass3/zbirnik/parsing/test.obj +++ b/ass3/zbirnik/parsing/test.obj @@ -0,0 +1,4 @@ +HPRG 00012C00001D +T00012C0F0100092500030300096B20053F2FFD +T00013D0C000001000002000003000001 +E00012C