<commit_before>$LOAD_PATH << "./"

require "tokens.rb"
require "symboltable.rb"
require "lexer.rb"
require "parser.rb"

def translator
  input = IO.read(ARGV[0])
  tree = parser(input)
  output = translate(tree)
  output += "\nbye"
  # write output to a file?
end

def translate(tree)
  ops = {
    'println'   => [:ibtl_println, 1],
    'print'     => [:ibtl_print, 1],
    '+'         => [:ibtl_plus, 2],
    '-'         => [:ibtl_minus, 2],
    'neg'       => [:ibtl_negate, 1],
    '*'         => [:ibtl_times, 2],
    '/'         => [:ibtl_divide, 2],
    '^'         => [:ibtl_power, 2],
    'and'       => [:ibtl_and, 2],
    'or'       => [:ibtl_or, 2],
    'iff'       => [:ibtl_iff, 2],
    'not'       => [:ibtl_not, 1],
  }

  output = ''

  tree = tree.to_enum
  loop do
    n = tree.next
    if n.is_a? Array
      output += translate(n) + ' '
    else
      begin
        key = n.value
      rescue NoMethodError
        key = n.tag
      end
      func, arg_count = ops[key]
      if func.nil?
        puts "Unknown token :#{n.tag} #{n.value} -- #{n}"
        exit 1
      end
      args = []
      arg_count.times do |i|
        a = tree.next
        if a.is_a? Array
          args << translate(a)
        else
          args << a
        end
      end
      output += method(func).call(*args) + ' '
    end
  end

  output
end

def to_gforth arg
  if arg.is_a? Token
    "#{arg.to_gforth}"
  else
    "#{arg.to_s}"
  end
end

def ibtl_noop(args)
  return ''
end

def ibtl_println(arg)
  arg = to_gforth arg
  "#{arg} . cr"
end

def ibtl_plus(arg0, arg1)
  arg0 = to_gforth arg0
  arg1 = to_gforth arg1
  "#{arg0} #{arg1} +"
end

def ibtl_minus(arg0, arg1)
  arg0 = to_gforth arg0
  arg1 = to_gforth arg1
  "#{arg0} #{arg1} -"
end

def ibtl_negate(arg)
  arg = to_gforth arg
  "0 #{arg} -"
end

def ibtl_times(arg0, arg1)
  arg0 = to_gforth arg0
  arg1 = to_gforth arg1
  "#{arg0} #{arg1} *"
end

def ibtl_divide(arg0, arg1)
  arg0 = to_gforth arg0
  arg1 = to_gforth arg1
  "#{arg0} #{arg1} /"
end

def ibtl_power(arg0, arg1)
  arg0 = to_gforth arg0
  arg1 = to_gforth arg1
  "#{arg0} #{arg1} ^"
end

def ibtl_concat(arg0, arg1)
  arg0 = to_gforth arg0
  arg1 = to_gforth arg1
  "#{arg0} #{arg1}"
end

def ibtl_and(arg0, arg1)
  arg0 = to_gforth arg0
  arg1 = to_gforth arg1
  "#{arg0} #{arg1} and"
end

def ibtl_or(arg0, arg1)
  arg0 = to_gforth arg0
  arg1 = to_gforth arg1
  "#{arg0} #{arg1} or"
end

def ibtl_iff(arg0, arg1)
  arg0 = to_gforth arg0
  arg1 = to_gforth arg1
  "#{arg0} #{arg1} ="
end

def ibtl_not(arg0)
  arg0 = to_gforth arg0
  "#{arg0} invert"
end

puts translator

#EOF vim: sw=2:ts=2<commit_msg>refactor translator and add float functions<commit_after>$LOAD_PATH << "./"

require "tokens.rb"
require "symboltable.rb"
require "lexer.rb"
require "parser.rb"

def translator
  input = IO.read(ARGV[0])
  tree = parser(input)
  output = translate(tree)
  output += "\nbye"
  # write output to a file?
end

def translate(tree)
  ops = {
    '+'         => [:ibtl_add, 2],    
    'and'       => [:ibtl_and, 2],    
    '/'         => [:ibtl_div, 2],    
    '='         => [:ibtl_eq, 2],     
    'e'         => [:ibtl_exp, 1],    
    'f+'        => [:ibtl_fadd, 2],   
    'f/'        => [:ibtl_fdiv, 2],   
    'f='        => [:ibtl_feq, 2],    
    'f<'        => [:ibtl_flt, 2],    
    'f-'        => [:ibtl_fminus, 2], 
    'f*'        => [:ibtl_fmult, 2],  
    'fneg'      => [:ibtl_fneg, 1],
    'f^'        => [:ibtl_fpower, 2], 
    'iff'       => [:ibtl_iff, 2],    
    '<'         => [:ibtl_lt, 2],     
    '-'         => [:ibtl_minus, 2],  
    '*'         => [:ibtl_mult, 2],   
    'neg'       => [:ibtl_neg, 1], 
    'not'       => [:ibtl_not, 1],    
    'or'        => [:ibtl_or, 2],     
    '^'         => [:ibtl_power, 2],  
    'print'     => [:ibtl_print, 1],  
    'println'   => [:ibtl_println, 1],
  }

  output = ''

  tree = tree.to_enum
  loop do
    n = tree.next
    if n.is_a? Array
      output += translate(n) + ' '
    else
      begin
        key = n.value
      rescue NoMethodError
        key = n.tag
      end
      func, arg_count = ops[key]
      if func.nil?
        puts "Unknown token :#{n.tag} #{n.value} -- #{n}"
        exit 1
      end
      args = []
      arg_count.times do |i|
        a = tree.next
        if a.is_a? Array
          args << translate(a)
        else
          args << a
        end
      end
      output += method(func).call(*args) + ' '
    end
  end

  output
end

def to_gforth arg
  if arg.is_a? Token
    "#{arg.to_gforth}"
  else
    "#{arg.to_s}"
  end
end

def ibtl_noop(args)
  return ''
end

def ibtl_add(arg0, arg1)
  arg0 = to_gforth arg0
  arg1 = to_gforth arg1
  "#{arg0} #{arg1} +"
end

def ibtl_and(arg0, arg1)
  arg0 = to_gforth arg0
  arg1 = to_gforth arg1
  "#{arg0} #{arg1} and"
end

def ibtl_concat(arg0, arg1)
  arg0 = to_gforth arg0
  arg1 = to_gforth arg1
  "#{arg0} #{arg1}"
end

def ibtl_div(arg0, arg1)
  arg0 = to_gforth arg0
  arg1 = to_gforth arg1
  "#{arg0} #{arg1} /"
end

def ibtl_eq(arg0, arg1)
  arg0 = to_gforth arg0
  arg1 = to_gforth arg1
  "#{arg0} #{arg1} ="
end

def ibtl_exp(arg0)
  arg0 = to_gforth arg0
  "#{arg0} exp"
end

def ibtl_fadd(arg0, arg1)
  arg0 = to_gforth arg0
  arg1 = to_gforth arg1
  "#{arg0} #{arg1} f+"
end

def ibtl_fdiv(arg0, arg1)
  arg0 = to_gforth arg0
  arg1 = to_gforth arg1
  "#{arg0} #{arg1} f/"
end

def ibtl_feq(arg0, arg1)
  arg0 = to_gforth arg0
  arg1 = to_gforth arg1
  "#{arg0} #{arg1} f="
end

def ibtl_flt(arg0, arg1)
  arg0 = to_gforth arg0
  arg1 = to_gforth arg1
  "#{arg0} #{arg1} f<"
end

def ibtl_fminus(arg0, arg1)
  arg0 = to_gforth arg0
  arg1 = to_gforth arg1
  "#{arg0} #{arg1} f-"
end

def ibtl_fmult(arg0, arg1)
  arg0 = to_gforth arg0
  arg1 = to_gforth arg1
  "#{arg0} #{arg1} f*"
end

def ibtl_fneg(arg0)
  arg0 = to_gforth arg0
  "0e #{arg0} f-"
end

def ibtl_fpower(arg0, arg1)
  arg0 = to_gforth arg0
  arg1 = to_gforth arg1
  "#{arg0} #{arg1} f**"
end

def ibtl_iff(arg0, arg1)
  arg0 = to_gforth arg0
  arg1 = to_gforth arg1
  "#{arg0} #{arg1} ="
end

def ibtl_lt(arg0, arg1)
  arg0 = to_gforth arg0
  arg1 = to_gforth arg1
  "#{arg0} #{arg1} <"
end

def ibtl_minus(arg0, arg1)
  arg0 = to_gforth arg0
  arg1 = to_gforth arg1
  "#{arg0} #{arg1} -"
end

def ibtl_mult(arg0, arg1)
  arg0 = to_gforth arg0
  arg1 = to_gforth arg1
  "#{arg0} #{arg1} *"
end

def ibtl_neg(arg)
  arg = to_gforth arg
  "0 #{arg} -"
end

def ibtl_not(arg0)
  arg0 = to_gforth arg0
  "#{arg0} invert"
end

def ibtl_or(arg0, arg1)
  arg0 = to_gforth arg0
  arg1 = to_gforth arg1
  "#{arg0} #{arg1} or"
end

def ibtl_power(arg0, arg1)
  arg0 = to_gforth arg0
  arg1 = to_gforth arg1
  "#{arg0}e #{arg1}e f**"
end

def ibtl_println(arg)
  arg = to_gforth arg
  "#{arg} . cr"
end

puts translator

#EOF vim: sw=2:ts=2
{'fraction_non_alphanumeric': 0.15478465382919301, 'fraction_numerical': 0.04086432456269293, 'mean_word_length': 2.296511627906977, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1917268', 'n_tokens_mistral': 3150, 'n_tokens_neox': 3066, 'n_words': 708}