1
This commit is contained in:
parent
1bbc80de29
commit
858db4dc0e
4 changed files with 109 additions and 29 deletions
|
|
@ -1,10 +1,16 @@
|
|||
(*tukaj je vse povezano s prvim prehodom zbirnika*)
|
||||
open SemanticAnalyzer
|
||||
open Simtab
|
||||
|
||||
let startAddr = ref 0
|
||||
let locctr = ref 0
|
||||
|
||||
let prviPrehod (code : lineSemantic list) =
|
||||
|
||||
(*funkcija prvega prehoda, !!! za END ne sme biti nič več definiranih ukazov ali direktiv!*)
|
||||
let prviPrehod (code : lineSemantic list) : int*lineSemantic list*symtab=
|
||||
(*inicializiramo startAddr, loctr in simtab*)
|
||||
let startAddr = ref 0 in
|
||||
let locctr = ref 0 in
|
||||
let simtab = create_symtab () in
|
||||
|
||||
let prviUkaz, ostaliUkazi =
|
||||
match code with
|
||||
| [] -> failwith "empty code"
|
||||
|
|
@ -12,34 +18,47 @@ let prviPrehod (code : lineSemantic list) =
|
|||
in
|
||||
|
||||
(*if Opcode == start then*)
|
||||
|
||||
if prviUkaz.opcode = START then
|
||||
let stAdr = get_string_from_mnemType prviUkaz.mnem in
|
||||
let stAdr = match stAdr with | Some s -> s | None -> "0" in
|
||||
startAddr := (int_of_string stAdr);
|
||||
locctr := !startAddr;
|
||||
(*TODO write line to intermediate file*)
|
||||
else
|
||||
(*initialize locctr to 0*)
|
||||
locctr := 0
|
||||
|
||||
;
|
||||
let intermediateFileList =
|
||||
if prviUkaz.opcode = START then
|
||||
let stAdr = get_string_from_mnemType prviUkaz.mnem in
|
||||
let stAdr = match stAdr with | Some s -> s | None -> "0" in
|
||||
startAddr := (int_of_string stAdr);
|
||||
locctr := !startAddr;
|
||||
(*write line to intermediate file*)
|
||||
prviUkaz.loc <- Some !locctr;
|
||||
[prviUkaz]
|
||||
else
|
||||
(*initialize locctr to 0*)
|
||||
(locctr := 0;
|
||||
[])
|
||||
in
|
||||
(*while opcode != END loop*)
|
||||
let rec loop ostaliUkazi =
|
||||
let rec loop ostaliUkazi prgLen intermediateFileList =
|
||||
match ostaliUkazi with
|
||||
| [] -> () (*smo konec*)
|
||||
| x :: _ when x.opcode = END -> () (*konec while loopa*) (*TODO write last line to intermediate file, save the locctr - st addr as prg len*)
|
||||
| x :: xs -> match x.opcode with
|
||||
| COMMENT -> loop xs (*if this is a comment line*)
|
||||
| [] -> prgLen, (List.rev intermediateFileList), simtab (*smo konec*)
|
||||
| x :: _ when x.opcode = END -> (*konec while loopa*) (*write last line to intermediate file, save the locctr - st addr as prg len*)
|
||||
x.loc <- Some !locctr;
|
||||
let intermediateFileList = x :: intermediateFileList in
|
||||
let prgLen = !locctr - !startAddr in
|
||||
prgLen, (List.rev intermediateFileList), simtab (*vrnemo program length*)
|
||||
| x :: xs -> match x.opcode with (*loopamo*)
|
||||
| COMMENT -> loop xs prgLen intermediateFileList(*if this is a comment line*)
|
||||
| _ ->
|
||||
(*if there is a symmbol in label field*)
|
||||
let s = match x.label with
|
||||
begin
|
||||
match x.label with
|
||||
| None -> ()
|
||||
| Some s -> () (*search simtab for s ...*)
|
||||
in
|
||||
| Some s ->
|
||||
(*search simtab for s ...*)
|
||||
(*doda simbol ce še ne obstaja, sier faila*)
|
||||
add_symbol simtab s !locctr
|
||||
end;
|
||||
|
||||
(*write line to intermediate file*)
|
||||
x.loc <- Some !locctr;
|
||||
(*search opcode -> smo ze naredili v semanticni analizi, tu bomo le ustrezno povečali locctr*)
|
||||
locctr := !locctr + x.len;
|
||||
(*TODO write line to intermediate file*)
|
||||
loop xs (*read nex input line end while*)
|
||||
in loop ostaliUkazi
|
||||
let intermediateFileList = x :: intermediateFileList in
|
||||
loop xs prgLen (intermediateFileList) (*read nex input line end while*)
|
||||
in loop ostaliUkazi 0 intermediateFileList
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue