User:Conor O'Brien/Compilers/Quartic

From Esolang
Jump to navigation Jump to search

Quartic is a language designed to compile to the language 4. Written in Ruby, the compiler can be found here, with a revised interpreter for the language.


Language Spec

Every line is a "statement"; each statement consists of one keyword and a list of parameters. Every parameter is one of the following:

an identifier     (matches /^[A-Za-z][A-Za-z0-9_]*$/)
a number          (matches /^\d+$/)
a character       (matches /'.'/)

The parameters are separated by spaces or commas.

Commands

Parameters marked with [ref] must be a named variable. Parameters marked with [val] may be either a named variable or a literal.

function(arity) param[0], param[1], param[2], ..., param[arity]

becomes

function a, b, c, ...
Command signature Psuedocode Additional notes
decl(N) ...target[ref] n/a Tells the processor that each of the variables will appear.
set(2) target[ref], value[val] target = value Like 6 in the original language.
print(N) ...target[ref] print(target[i]) Parameters can appear multiple times in a single statement.
add(3) target[ref], opa[val], opb[val] target = opa + opb Like 0 in the original language.
addx(2) target[ref], op[val] target += op none
inc(1) target[ref] target++ none
sub(3) target[ref], opa[val], opb[val] target = opa - opb Like 1 in the original language.
subx(2) target[ref], op[val] target -= op none
dec(1) target[ref] target-- none
zero(1) target[ref] target = 0 Set with 0.
neg(1) target[ref] target = -target Subtraction with 0 and the variable.
mul(3) target[ref], opa[val], opb[val] target = opa * opb Like 2 in the original language.
mulx(2) target[ref], op[val] target *= op none
square(1) target[ref] target *= target none
div(3) target[ref], opa[val], opb[val] target = opa / opb Like 3 in the original language.
divx(2) target[ref], op[val] target /= op none
cmp(3) target[ref], opa[val], opb[val] target = opa == opb Target is 1 when the two parameters are equal, 0 otherwise.
input(1) target[ref] target = input() Like 7 in the original language.

There are also loop target[ref], if target[ref], and end.

Example programs

Deadfish interpreter

Character output.

decl c, equal, acc, n1
set n1, 0
dec n1

input c

loop c
  ; increment
  cmp equal, c, 'i'
  if equal
    inc acc
  end
  
  ; decrement
  cmp equal, c, 'd'
  if equal
    dec acc
  end
  
  ; square
  cmp equal, c, 's'
  if equal
    square acc
  end
  
  ; output
  cmp equal, c, 'o'
  if equal
    ; todo: actual print
    print acc
  end
  
  zero c
  cmp equal, acc, n1
  if equal
    set c, 1
  end
  cmp equal, acc, 256
  if equal
    set c, 1
  end
  
  if c
    zero acc
  end
  
  
  input c
end

This compiles to:

3.60001602996010600101026030160599604010040405606016089960716007070861099609120090910612996139961158011111201111136230012323007208206900009090201909001290909062101890621006900096600006060218600222203660009690000909020190900429090906210189062100690009661000616121861122220666100969000090902019090072909090621018906210069000966200062622186222222226620096900009090201909009290909062101890621006900096630006363218635226630096200069000090902219090232909090621018906210069000966400064642186462001664009690000909022190901129090906210189062100690009665000656521865620016650096660006666208666220066600972094