daily pastebin goal
23%
SHARE
TWEET

Untitled

a guest Mar 22nd, 2019 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from os import exists_file, param_count, param_str
  2. import niacompile
  3. import parseopt
  4. import streams
  5. import niavm
  6.  
  7. when is_main_module:
  8.   var compile: bool;
  9.  
  10.   let argc = param_count()
  11.   assert(argc > 2, "Invalid arguments count.")
  12.  
  13.   let argument = param_str(1)
  14.  
  15.   case argument:
  16.   of "--compile", "-c":
  17.     assert(argc >= 3, "Filename expected.")
  18.  
  19.     let input_file = param_str(2)
  20.     assert(input_file.ends_with(".nia"), ".nia file expected, got " & input_file ".")
  21.  
  22.     if exists_file(input_file):
  23.       let output_file = input_file.replace(".nia", ".niabin")
  24.  
  25.       let code = read_file(input_file)
  26.       let tok = tokenize(code)
  27.       let bcode = compile(tok)
  28.  
  29.       var stream = new_file_stream(output_file, fm_write)
  30.       for i in bcode:
  31.         stream.write(i)
  32.       stream.close()
  33.     else:
  34.       assert(false, "Could not open file " & input_file & ".")
  35.   of "--help", "-h":
  36.     echo """
  37.     Compilation:
  38.       nia -c <filename>
  39.  
  40.       You can only compile .nia files!
  41.  
  42.     Execution:
  43.       nia <filename>
  44.  
  45.       You can only execute .niabin files!
  46.     """
  47.   else:
  48.     assert(argument.ends_with(".niabin"), ".niabin file expected, got " & argument ".")
  49.  
  50.     var bcode: seq[float] = @[]
  51.  
  52.     var stream = new_file_stream(argument, fm_read)
  53.     while not stream.at_end():
  54.       bcode.add(float(stream.read_float64()))
  55.     stream.close()
  56.  
  57.     var vm = newNIAVM(bcode, false)
  58.     vm.interpret()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top