Анимация
JavaScript
|
Главная Библионтека { Recognize a Boolean Orop } function IsOrop(c: char): boolean; begin IsOrop := c in [j, -]; end; { Recognize a Relop } function IsRelop(c: char): boolean; begin IsRelop := c in [=, #, <, >]; end; { Recognize White Space } function IsWhite(c: char): boolean; begin IsWhite := c in [ , TAB]; end; { Skip Over Leading White Space } procedure SkipWhite; begin while IsWhite(Look) do GetChar; end; { Skip Over an End-of-Line } procedure Fin; begin if Look = CR then begin GetChar; if Look = LF then GetChar; end; end; { Match a Specific Input Character } procedure Match(x: char); begin if Look = x then GetChar else Expected( + x + ); SkipWhite; end; { Get an Identifier } function GetName: char; begin if not IsAlpha(Look) then Expected(Name); GetName := UpCase(Look); GetChar; SkipWhite; end; { Get a Number } function GetNum: char; begin if not IsDigit(Look) then Expected(Integer); GetNum := Look; GetChar; SkipWhite; end; { Output a String with Tab } procedure Emit(s: string); begin Write(TAB, s); end; { Output a String with Tab and CRLF } procedure EmitLn(s: string); begin Emit(s); WriteLn; end; { Post a Label To Output } procedure PostLabel(L: string); begin WriteLn(L, :); end; { Load a Variable to the Primary Register } procedure LoadVar(Name: char); begin CheckVar(Name); EmitLnCMOVE + Name + (PC),D0); end; { Store the Primary Register } procedure StoreVar(Name: char); begin CheckVar(Name); EmitLnCLEA + Name + (PC),A0); EmitLnCMOVE D0,(A0)) end; { Initialize } procedure Init; var i: char; begin GetChar; SkipWhite; for i := A to Z do ST[i] := ; end; { Parse and Translate an Expression } { Vestigial Version } procedure Expression; begin LoadVar(GetName); end; { Parse and Translate an Assignment Statement } procedure Assignment; var Name: char; begin Name := GetName; MatchC = ); Expression; StoreVar(Name); end; { Parse and Translate a Block of Statements } procedure DoBlock; begin while not(Look in [e]) do begin Assignment; Fin; end; end; { Parse and Translate a Begin-Block } procedure BeginBlock; begin Match(b); Fin; DoBlock; Match(e); Fin; end; { Allocate Storage for a Variable } procedure Alloc(N: char); begin if InTable(N) then Duplicate(N); ST[N] := v; WriteLn(N, TAB, DC 0); end; { Parse and Translate a Data Declaration } procedure Decl; var Name: char; begin Match(v); Alloc(GetName); end; { Parse and Translate Global Declarations } procedure TopDecls; begin while Look <> b do begin case Look of v: Decl; else Abort(Unrecognized Keyword + Look); end; Fin; end; end; { Main Program } begin Init; TopDecls; BeginBlock; end. Обратите внимание, что у нас есть таблица идентификаторов и есть логика для проверки допустимости имени переменной. Также стоит обратить внимание на то, что я 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 [ 68 ] 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |