Lines

From Esolang
Jump to navigation Jump to search

Lines is a language based on ///, but with input.

Specification

The program is interpreted exactly as in ///, with one exception. If \| is encountered, rather than doing something with |, the program gets one character from the standard input.

Examples

All of the examples for /// are valid in Lines, as long as they do not contain | anywhere.

Read a character and print it

\|

Read a character and print it twice

/~/\|/~~

(Both this and the next program freeze if the user inputs a ~.)

Read a character and print it, unless it is a q

/~/\|//q//~

Interpreter

import java.util.Scanner;
public class Lines {
    public static void run(String prog) {
        Scanner input = new Scanner(System.in);
        input.useDelimiter("");
        StringBuilder program = new StringBuilder(prog);
        program:
        while (true) {
            while (true) {
                if (program.length() == 0) {
                    break program;
                }
                if (program.charAt(0) == '\\') {
                    char next = program.charAt(1);
                    if (next == '|') {
                        next = input.next().charAt(0);
                    }
                    System.out.print(next);
                    program.delete(0, 2);
                } else if (program.charAt(0) == '/') {
                    program.deleteCharAt(0);
                    break;
                } else {
                    System.out.print(program.charAt(0));
                    program.deleteCharAt(0);
                }
            }
            StringBuilder pattern = new StringBuilder();
            while (true) {
                if (program.length() == 0) {
                    break program;
                }
                if (program.charAt(0) == '\\') {
                    char next = program.charAt(1);
                    if (next == '|') {
                        next = input.next().charAt(0);
                    }
                    pattern.append(next);
                    program.delete(0, 2);
                } else if (program.charAt(0) == '/') {
                    program.deleteCharAt(0);
                    break;
                } else {
                    pattern.append(program.charAt(0));
                    program.deleteCharAt(0);
                }
            }
            StringBuilder replacement = new StringBuilder();
            while (true) {
                if (program.length() == 0) {
                    break program;
                }
                if (program.charAt(0) == '\\') {
                    char next = program.charAt(1);
                    if (next == '|') {
                        next = input.next().charAt(0);
                    }
                    replacement.append(next);
                    program.delete(0, 2);
                } else if (program.charAt(0) == '/') {
                    program.deleteCharAt(0);
                    break;
                } else {
                    replacement.append(program.charAt(0));
                    program.deleteCharAt(0);
                }
            }
            String patternStr = pattern.toString();
            String replaceStr = replacement.toString();
            int index = program.indexOf(patternStr);
            while (index != -1) {
                program.replace(index, index + pattern.length(), replaceStr);
                index = program.indexOf(patternStr);
            }
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String line;
        while (!(line = scanner.nextLine()).isEmpty()) {
            run(line);
            System.out.println();
        }
    }
}

See also

itflabtijtslwi