Advertisement
Guest User

Untitled

a guest
Jun 2nd, 2015
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Nim 3.21 KB | None | 0 0
  1. #Constant Declarations
  2. const
  3.   TAB   = '\t'
  4.   CR    = '\r'
  5.  
  6. #Variable Declarations
  7. var look: char              #Lookahead Character
  8.  
  9. #Read New Character From Input Stream
  10. proc getChar = look = stdin.readChar()
  11.  
  12. #Report an Error
  13. proc error(s: string) = echo '\l', "Error: ", s, '.'
  14.  
  15. #Report Error and Halt
  16. proc abort(s: string) =
  17.   error s
  18.   quit()
  19.  
  20. #Report What Was Expected
  21. proc expected(s: string) = abort s & " Expected"
  22.  
  23. #Recognize White Space
  24. proc isWhite(c: char): bool = c in [' ', TAB]
  25.  
  26. #Skip Over Leading White Space
  27. proc skipWhite =
  28.   while isWhite look: getChar()
  29.  
  30. #Match a Specific Input Character
  31. proc match(x: char) =
  32.   if look != x: expected '\"' & x & '\"'
  33.  else:
  34.    getChar()
  35.    skipWhite()
  36.  
  37. #Recognize an Alpha Character
  38. import strutils
  39. proc isAlpha(c: char): bool = (toUpper c) in 'A'..'Z'
  40.  
  41. #Recognize a Decimal Digit
  42. proc isDigit(c: char): bool = c in '0'..'9'
  43.  
  44. #Recognize an Addop
  45. proc isAddop(c: char): bool = c in ['+', '-']
  46.  
  47. #Recognize an Alphanumeric
  48. proc isAlNum(c: char): bool = (isAlpha c) or (isDigit c)
  49.  
  50. #Get an Identifier
  51. proc getName: string =
  52.  if not isAlpha look: expected "Name"
  53.  result = ""
  54.  while isAlNum look:
  55.    add result, (toUpper look)
  56.    getChar()
  57.  skipWhite()
  58.  
  59. #Get a Number
  60. proc getNum: string =
  61.  if not isDigit look: expected "Integer"
  62.  result = ""
  63.  while isDigit look:
  64.    add result, look
  65.    getChar()
  66.  skipWhite()
  67.  
  68. #Output a String with Tab
  69. proc emit(s: string) = echo TAB, s
  70.  
  71. #Output a String with Tab and CRLF
  72. proc emitLn(s: string) =
  73.  emit s
  74.  echo ' '
  75.  
  76. #Initialize
  77. proc init =
  78.  getChar()
  79.  skipWhite()
  80.  
  81. #Parse and Translate an Identifier
  82. proc ident =
  83.  let name = getName()
  84.  if look == '(':
  85.    match '('
  86.    match ')'
  87.    emitLn "BSR " & name
  88.  else: emitLn "MOVE " & name & "(PC), D0"
  89.  
  90. proc expression     #Forward dec
  91.  
  92. #Parse and Translate a Math Factor
  93. proc factor =
  94.  if look == '(':
  95.    match '('
  96.    expression()
  97.    match ')'
  98.  elif isAlpha look: ident()
  99.  else: emitLn "MOVE #" & getNum() & ", D0"
  100.  
  101. #Recognize and Translate a Multiply
  102. proc multiply =
  103.   match '*'
  104.   factor()
  105.   emitLn "MULS (SP)+, D0"
  106.  
  107. #Recognize and Translate a Divide
  108. proc divide =
  109.   match '/'
  110.   factor()
  111.   emitLn "MOVE (SP)+, D1"
  112.   emitLn "DIV D1, D0"
  113.    
  114. #Parse and Translate a Math Term
  115. proc term =
  116.   factor()
  117.   while look in ['*', '/']:
  118.     emitLn "MOVE D0, -(SP)"
  119.     case look
  120.     of '*': multiply()
  121.     of '/': divide()
  122.     else: expected "Mulop"
  123.  
  124. #Recognize and Translate an Add
  125. proc add =
  126.   match '+'
  127.   term()
  128.   emitLn "ADD (SP)+, D0"
  129.  
  130. #Recognize and Translate a Subtract
  131. proc subtract =
  132.   match '-'
  133.   term()
  134.   emitLn "SUB (SP)+, D0"
  135.   emitLn "NEG D0"
  136.    
  137. #Parse and Translate an Expression
  138. proc expression =
  139.   if isAddop look: emitLn "CLR D0"
  140.   else: term()
  141.   while isAddop look:
  142.     emitLn "MOVE D0, -(SP)"
  143.     case look:
  144.     of '+': add()
  145.     of '-': subtract()
  146.     else: expected "Addop"
  147.  
  148. #Parse and Translate an Assignment Statement
  149. proc assigment =
  150.   let name = getName()
  151.   match '='  
  152.   expression()
  153.   emitLn "LEA " & name & "(PC), A0"
  154.   emitLn "MOVE D0, (A0)"
  155.    
  156. #Main Program
  157. init()
  158. assigment()
  159. if look != CR: expected "NewLine"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement