Niseg

simple preprocessor

Apr 22nd, 2013
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.83 KB | None | 0 0
  1. defList={};
  2. local fout;
  3. function parsePoundStatement(str)
  4. local val,a,b,c,d;
  5.     for val,a,b,d,c in string.gmatch(str,"#(%a+)(%s+)(%a+)(%s+)(.-)$") do
  6.         if val=="define" and a and b and c then
  7.             defList[b]=c;
  8. --          print(b.." == "..defList[b])
  9.         end
  10.     end
  11. end
  12.  
  13. function parseRegularStatement(line)
  14. local oline=line
  15.     line =line.." "
  16.     for text, expr  in string.gmatch(line, "([%a_]+)([%s;,:\(\)]+)") do
  17.     if  defList[text]~=nil then
  18. --print(text.." found")
  19.     oline=string.gsub(oline,text,defList[text],1);
  20.     else
  21. -- print(text.." not found")
  22.     end
  23.  
  24.     end
  25.     fout.writeLine(oline)
  26. --print(oline);
  27. return oline
  28. end
  29.  
  30. function readMacroFile(fn)
  31. if fn== nil  then fn="macros"  end
  32. if not fs.exists(fn) then
  33. shell.run("pastebin get ycMm8L5N "..fn)
  34. end
  35. local f= fs.open(fn,"r")
  36. local line;
  37. repeat
  38. line= f.readLine()
  39. if(line~=nil) then
  40.     if string.gmatch(line,"^#(.*)")~=nil then
  41.     parsePoundStatement(line)
  42.     end
  43. end
  44. until  line==nil
  45.  
  46. end
  47. readMacroFile()
  48. --[[
  49. parsePoundStatement("#define rpt repeat")
  50. parsePoundStatement("#define utl until")
  51. parsePoundStatement("#define els else")
  52. parsePoundStatement("#define eif elseif")
  53. parsePoundStatement("#define whi while")
  54. parsePoundStatement("#define tpf turtle.place()")
  55. parsePoundStatement("#define tpd turtle.placeDown()")
  56. parsePoundStatement("#define tpu turtle.placeUp()")
  57. parsePoundStatement("#define tmf turtle.forward()")
  58. parsePoundStatement("#define tmd turtle.down()")
  59. parsePoundStatement("#define tmu turtle.up()")
  60. parsePoundStatement("#define tmb turtle.back()")
  61. parsePoundStatement("#define ttl turtle.turnLeft()")
  62. parsePoundStatement("#define ttr turtle.turnRight()")
  63. parsePoundStatement("#define txf turtle.dig()")
  64. parsePoundStatement("#define txu turtle.digUp()")
  65. parsePoundStatement("#define txd turtle.digDown()")
  66. parsePoundStatement("#define tdf turtle.drop()")
  67. parsePoundStatement("#define tdd turtle.dropDown()")
  68. parsePoundStatement("#define tdu turtle.dropUp()")
  69. --]]
  70. local str=""
  71. local k=1;
  72. local args={...}
  73. function help()
  74.  
  75. print("<object><function><dirction>")
  76. print("objects [r]edstone [t]urtle>")
  77. print("redstone :[g]et [s]et [c]lear")
  78. print("turtle: x=dig [m]ove [d]rop [p]lace")
  79. print("l=detect sl=select [c]ompare")
  80.  
  81. end
  82.  
  83. if #args ==0 then
  84. help()
  85. while true do
  86. print("entering string mode")
  87. fout=fs.open("tempscr"..k,"w")
  88. str=read();
  89. if(str=="quit") then break  
  90. elseif str=="help" then help() end
  91.  
  92. --print("str="..str)
  93. parseRegularStatement(str)
  94. fout.close()
  95.  
  96. shell.run("tempscr"..k)
  97. k=k+1
  98. end
  99. else
  100. local fnin=args[1]
  101. local fnout=args[2]
  102. local fin
  103. if fnout ==nil then fnout="outscr"end
  104. if not fs.exists(fnin) then print(fnin.." not found"); error() end
  105. fin=fs.open(fnin,"r")
  106. fout=fs.open(fnout,"w")
  107. while true do
  108. str=fin.readLine()
  109. if str~=nil then parseRegularStatement(str)
  110. else break end
  111.  
  112. end
  113. fin.close()
  114. fout.close()
  115.  
  116. end
Advertisement
Add Comment
Please, Sign In to add comment