JSInstruction

From Esolang
Jump to navigation Jump to search

JSInstruction is an esoteric programming language written by User:Dominicentek. The language has only one built in instruction and you can add more by including JavaScript code.

Information

As previously said, there is only one built in instruction. define and the text after it is used to define a JavaScript file containing the instructions and its code.

Functions

program.set(string, value) - Sets/adds a variable
program.get(string) - Gets a variable and returns it. undefined if the variable is not defined.
program.gotoLine(number) - Goes to a line number
program.print(string) - Prints out a message to console
program.println(string) - Prints out a message to console with a new line
program.input() - Asks user for input, returns a string containing the input
program.exit() - Exits the program

Value passed into the set function is converted into a string, then to a number (if it can).
program object is only defined in the functions. If accessed from outside of any functions, it returns undefined
console object is always undefined

How to define instructions

Instructions are defined as functions of the same name, with parameters being the instruction's parameter. A parameter can either be a string or a number. With constant string being surrounded by double quotes (") and constant numbers being the numbers themselves. Strings can not surrounded by double quotes (") are treated as normal strings also, space terminates them. Parameters of the function that are not defined in the instructions are null.

Examples

Hello, World

define instructions.js
print "Hello, World!"

instructions.js:

function print(message) {
  program.print(message)
}

truth-machine

define instructions.js
input x
if_is x 0 6
print 1
goto 4
print 0

instructions.js:

function input(variable) {
  program.set(variable, program.input())
}
function if_is(variable, value, lineNum) {
  if (program.get(variable) == value) program.gotoLine(lineNum)
}
function print(message) {
  program.print(message)
}
function goto(lineNum) {
  program.gotoLine(lineNum)
}

Calculator

define instructions.js
input x
input operation
input y
math x operation y
print x

instructions.js:

function input(variable) {
  program.set(variable, program.input())
}
function math(x, op, y) {
  var left = program.get(x)
  var right = program.get(y)
  if (op == "+") program.set(x, left + right)
  if (op == "-") program.set(x, left - right)
  if (op == "*") program.set(x, left * right)
  if (op == "/") program.set(x, left / right)
  if (op == "%") program.set(x, left % right)
}
function print(value) {
  program.print("" + program.get(value))
}

Interpreter

Download