Inverted Logic/Interpreter
Jump to navigation
Jump to search
This code is written in Java.
import java.util.Scanner; public class InvertedLogic { public static void main (String args[]) { Scanner input = new Scanner(System.in); String code = ""; String result = ""; int lineNumber = 0; while (!(code.contains(" exit ") || code.startsWith("exit ") || code.endsWith(" exit") || code.equals("exit"))) { try { lineNumber--; code = input.nextLine().trim(); code = code.replaceAll(" +;.*", ""); code = code.replaceAll(" +", " "); if ((code.contains(" and ") || code.startsWith("and ") || code.endsWith(" and") || code.equals("and")) || (code.contains(" nand ") || code.startsWith("nand ") || code.endsWith(" nand") || code.equals("nand")) || (code.contains(" xor ") || code.startsWith("xor ") || code.endsWith(" xor") || code.equals("xor")) || ( code.contains(" xnor ") || code.startsWith("xnor ") || code.endsWith(" xnor") || code.equals("xnor")) || (code.contains(" or ") || code.startsWith("or ") || code.endsWith(" or") || code.equals("or")) || (code.contains(" nor ") || code.startsWith("nor ") || code.endsWith(" nor") || code.equals("nor")) || ( code.contains(" true ") || code.startsWith("true ") || code.endsWith(" true") || code.equals("true")) || (code.contains(" false ") || code.startsWith("false ") || code.endsWith(" false") || code.equals("false")) || ((code.startsWith("print ") || code.equals("print")) || (code.startsWith("println ") || code.equals("println")) || ((code.startsWith("printsp ") || code.equals("printsp"))) || code.equals("keywords") || code.equals("errorcodes") || (code.contains(";") || code.startsWith(";") || code.endsWith(";") || code.equals(";")) || (code.contains(" exit ") || code.startsWith("exit ") || code.endsWith(" exit") || code.equals("exit")))) { if ( code.startsWith(";") || code.equals(";")) {} if (code.contains(";") || code.endsWith(";")) {} if ((code.contains(" and ") || code.startsWith("and ") || code.endsWith(" and") || code.equals("and")) || (code.contains(" nand ") || code.startsWith("nand ") || code.endsWith(" nand") || code.equals("nand")) || (code.contains(" xor ") || code.startsWith("xor ") || code.endsWith(" xor") || code.equals("xor")) || ( code.contains(" xnor ") || code.startsWith("xnor ") || code.endsWith(" xnor") || code.equals("xnor")) || (code.contains(" or ") || code.startsWith("or ") || code.endsWith(" or") || code.equals("or")) || (code.contains(" nor ") || code.startsWith("nor ") || code.endsWith(" nor") || code.equals("nor"))) { while (( code.contains(" and ") || code.startsWith("and ") || code.endsWith(" and") || code.equals("and")) || (code.contains(" nand ") || code.startsWith("nand ") || code.endsWith(" nand") || code.equals("nand")) || (code.contains(" xor ") || code.startsWith("xor ") || code.endsWith(" xor") || code.equals("xor")) || ( code.contains(" xnor ") || code.startsWith("xnor ") || code.endsWith(" xnor") || code.equals("xnor")) || (code.contains(" or ") || code.startsWith("or ") || code.endsWith(" or") || code.equals("or")) || (code.contains(" nor ") || code.startsWith("nor ") || code.endsWith(" nor") || code.equals("nor"))) { if (code.contains(" and ") || code.startsWith("and ") || code.endsWith(" and") || code.equals("and")) { if (code.contains(" true and true ") || code.startsWith( "true and true ") || code.endsWith(" true and true") || code.equals("true and true") || code.contains(" true and false ") || code.startsWith("true and false ") || code.endsWith(" true and false") || code.equals("true and false") || code.contains(" false and true ") || code.startsWith("false and true ") || code.endsWith( " false and true") || code.equals("false and true") || code.contains(" false and false ") || code.startsWith("false and false ") || code.endsWith( " false and false") || code.equals("false and false")) { if (code.contains(" true and true ") || code.startsWith("true and true ") || code.endsWith( " true and true") || code.equals("true and true")) { result = "false"; code = code.replaceFirst("true and true", result); } if (code.contains( " true and false ") || code.startsWith("true and false ") || code.endsWith(" true and false") || code.equals("true and false")) { result = "true"; code = code.replaceFirst("true and false", result); } if (code.contains(" false and true ") || code.startsWith("false and true ") || code.endsWith( " false and true") || code.equals("false and true")) { result = "true"; code = code.replaceFirst("false and true", result); } if (code.contains( " false and false ") || code.startsWith("false and false ") || code.endsWith(" false and false") || code.equals("false and false")) { result = "true"; code = code.replaceFirst("false and false", result); }} else { System.out.println("Program error:\n\n\'and\' is used with less than 2 values"); System.exit(203); } } if (code.contains(" nand ") || code.startsWith("nand ") || code.endsWith(" nand") || code.equals("nand")) { if (code.contains(" true nand true ") || code.startsWith("true nand true ") || code.endsWith(" true nand true") || code.equals("true nand true") || code.contains(" true nand false ") || code.startsWith( "true nand false ") || code.endsWith(" true nand false") || code.equals("true nand false") || code.contains(" false nand true ") || code.startsWith( "false nand true ") || code.endsWith(" false nand true") || code.equals("false nand true") || code.contains(" false nand false ") || code.startsWith( "false nand false ") || code.endsWith(" false nand false") || code.equals("false nand false")) { if (code.contains(" true nand true ") || code.startsWith( "true nand true ") || code.endsWith(" true nand true") || code.equals("true nand true")) { result = "true"; code = code.replaceFirst("true nand true", result ); } if (code.contains(" true nand false ") || code.startsWith("true nand false ") || code.endsWith(" true nand false") || code.equals("true nand false")) { result = "false"; code = code.replaceFirst("true nand false", result); } if (code.contains(" false nand true ") || code.startsWith("false nand true ") || code.endsWith(" false nand true") || code.equals("false nand true")) { result = "false"; code = code.replaceFirst("false nand true", result); } if ( code.contains(" false nand false ") || code.startsWith("false nand false ") || code.endsWith(" false nand false") || code.equals("false nand false")) { result = "false"; code = code.replaceFirst("false nand false", result); } else { System.out.println( "Program error:\n\n\'nand\' is used with less than 2 values"); System.exit(203); } }} if (code.contains(" xor ") || code.startsWith("xor ") || code.endsWith(" xor") || code.equals("xor")) { if (code.contains(" true xor true ") || code.startsWith( "true xor true ") || code.endsWith(" true xor true") || code.equals("true xor true") || code.contains(" true xor false ") || code.startsWith("true xor false ") || code.endsWith(" true xor false") || code.equals("true xor false") || code.contains(" false xor true ") || code.startsWith("false xor true ") || code.endsWith( " false xor true") || code.equals("false xor true") || code.contains(" false xor false ") || code.startsWith("false xor false ") || code.endsWith( " false xor false") || code.equals("false xor false")) { if (code.contains(" true xor true ") || code.startsWith("true xor true ") || code.endsWith( " true xor true") || code.equals("true xor true")) { result = "true"; code = code.replaceFirst("true xor true", result); } if (code.contains( " true xor false ") || code.startsWith("true xor false ") || code.endsWith(" true xor false") || code.equals("true xor false")) { result = "false"; code = code.replaceFirst("true xor false", result); } if (code.contains(" false xor true ") || code.startsWith("false xor true ") || code.endsWith(" false xor true" ) || code.equals("false xor true")) { result = "false"; code = code.replaceFirst("false xor true", result); } if (code.contains(" false xor false ") || code.startsWith("false xor false ") || code.endsWith(" false xor false") || code.equals("false xor false")) { result = "true"; code = code.replaceFirst( "false xor false", result); }} else { System.out.println("Program error:\n\n\'xor\' is used with less than 2 values"); System.exit(203); } } if (code.contains(" xnor ") || code.startsWith("xnor ") || code.endsWith(" xnor") || code.equals("xnor")) { if (code.contains(" true xnor true ") || code.startsWith("true xnor true ") || code.endsWith(" true xnor true") || code.equals("true xnor true") || code.contains(" true xnor false ") || code.startsWith( "true xnor false ") || code.endsWith(" true xnor false") || code.equals("true xnor false") || code.contains(" false xnor true ") || code.startsWith( "false xnor true ") || code.endsWith(" false xnor true") || code.equals("false xnor true") || code.contains(" false xnor false ") || code.startsWith( "false xnor false ") || code.endsWith(" false xnor false") || code.equals("false xnor false")) { if (code.contains(" true xnor true ") || code.startsWith( "true xnor true ") || code.endsWith(" true xnor true") || code.equals("true xnor true")) { result = "false"; code = code.replaceFirst("true xnor true", result); } if (code.contains(" true xnor false ") || code.startsWith("true xnor false ") || code.endsWith(" true xnor false") || code.equals( "true xnor false")) { result = "true"; code = code.replaceFirst("true xnor false", result); } if (code.contains(" false xnor true ") || code.startsWith( "false xnor true ") || code.endsWith(" false xnor true") || code.equals("false xnor true")) { result = "true"; code = code.replaceFirst("false xnor true", result); } if (code.contains(" false xnor false ") || code.startsWith("false xnor false ") || code.endsWith(" false xnor false") || code.equals( "false xnor false")) { result = ""; code = code.replaceFirst("false xnor false", result); } else { System.out.println( "Program error:\n\n\'xnor\' is used with less than 2 values"); System.exit(203); } }} if (code.contains(" or ") || code.startsWith("or ") || code.endsWith(" or") || code.equals("or")) { if (code.contains(" true or true ") || code.startsWith( "true or true ") || code.endsWith(" true or true") || code.equals("true or true") || code.contains(" true or false ") || code.startsWith("true or false ") || code.endsWith(" true or false") || code.equals("true or false") || code.contains(" false or true ") || code.startsWith("false or true ") || code.endsWith( " false or true") || code.equals("false or true") || code.contains(" false or false ") || code.startsWith("false or false ") || code.endsWith(" false or false") || code.equals("false or false")) { if (code.contains(" true or true ") || code.startsWith("true or true ") || code.endsWith( " true or true") || code.equals( "true or true")) { result = "false"; code = code.replaceFirst("true or true", result); } if (code.contains(" true or false ") || code.startsWith( "true or false ") || code.endsWith(" true or false") || code.equals("true or false")) { result = "false"; code = code.replaceFirst("true or false", result); } if (code.contains(" false or true ") || code.startsWith("false or true ") || code.endsWith(" false or true") || code.equals("false or true")) { result = "false"; code = code.replaceFirst("false or true", result); } if (code.contains(" false or false ") || code.startsWith("false or false ") || code.endsWith( " false or false") || code.equals("false or false")) { result = "true"; code = code.replaceFirst("false or false", result); }} else { System.out.println( "Program error:\n\n\'or\' is used with less than 2 values"); System.exit(203); } } if (code.contains(" nor ") || code.startsWith("nor ") || code.endsWith(" nor") || code.equals("nor")) { if (code.contains(" true nor true ") || code.startsWith( "true nor true ") || code.endsWith(" true nor true") || code.equals("true nor true") || code.contains(" true nor false ") || code.startsWith("true nor false ") || code.endsWith(" true nor false") || code.equals("true nor false") || code.contains(" false nor true ") || code.startsWith("false nor true ") || code.endsWith( " false nor true") || code.equals("false nor true") || code.contains(" false nor false ") || code.startsWith("false nor false ") || code.endsWith( " false nor false") || code.equals("false nor false")) { if (code.contains(" true nor true ") || code.startsWith("true nor true ") || code.endsWith( " true nor true") || code.equals("true nor true")) { result = "true"; code = code.replaceFirst("true nor true", result); } if (code.contains( " true nor false ") || code.startsWith("true nor false ") || code.endsWith(" true nor false") || code.equals("true nor false")) { result = "true"; code = code.replaceFirst("true nor false", result); } if (code.contains(" false nor true ") || code.startsWith("false nor true ") || code.endsWith(" false nor true" ) || code.equals("false nor true")) { result = "true"; code = code.replaceFirst("false nor true", result); } if (code.contains(" false nor false ") || code.startsWith("false nor false ") || code.endsWith(" false nor false") || code.equals("false nor false")) { result = "false"; code = code.replaceFirst( "false nor false", result); } else { System.out.println("Program error:\n\n\'nor\' is used with less than 2 values"); System.exit(203); }}}}} else { if (code.contains(" true ") || code.startsWith("true ") || code.endsWith(" true") || code.equals("true")) { while (code.contains(" true ") || code.startsWith("true " ) || code.endsWith(" true") || code.equals("true")) { result = "false"; System.out.println(result); }} else if (code.contains(" false ") || code.startsWith("false ") || code.endsWith(" false") || code.equals("false")) { while (code.contains(" false ") || code.startsWith("false ") || code.endsWith(" false") || code.equals("false") ) { result = "true"; System.out.println(result); }} } if (code.startsWith("print ") || code.equals("print")) { if (code.startsWith("print ")) { result = code.subSequence(6, code.length()).toString(); result = result.replace("true", ""); result = result.replace("false", ""); result = result.replace("and", ""); result = result.replace("nand", ""); result = result.replace( "xor", ""); result = result.replace("xnor", ""); result = result.replace("or", ""); result = result.replace("nor", ""); result = result.replaceAll(" +;.*", ""); System.out.print(result); } else if (code.equals("print")) { result = ""; System.out.print(result); } } else if (code.startsWith("printsp ") || code.equals("printsp")) { if (code.startsWith("printsp ")) { result = code.subSequence(8, code.length()).toString(); result = result.replace("true", ""); result = result.replace("false", ""); result = result.replace("and", ""); result = result.replace("nand", ""); result = result.replace( "xor", ""); result = result.replace("xnor", ""); result = result.replace("or", ""); result = result.replace("nor", ""); result = result.replaceAll(" +;.*", ""); System.out.print(result.concat(" ")); } else if (code.equals("printsp")) { result = " "; System.out.println(result); } } else if (code.startsWith("println ") || code.equals("println")) { if (code.startsWith("println ")) { result = code.subSequence(8, code.length()).toString(); result = result.replace("true", ""); result = result.replace("false", ""); result = result.replace("and", ""); result = result.replace("nand", ""); result = result.replace( "xor", ""); result = result.replace("xnor", ""); result = result.replace("or", ""); result = result.replace("nor", ""); result = result.replaceAll(" +;.*", ""); System.out.println(result); } } else if (code.equals("println")) { result = ""; System.out.println(result); } if (code.equals("keywords")) { result = ""; System.out.print("All keywords:\n\nand\nnand\nxor\nxnor\nor\nnor\ntrue\nfalse\nprint\nprintln\nprintsp\nkeywords\nerro"); System.out.print("rcodes"); } else if (code.equals("errorcodes")) { result = ""; System.out.print("All error codes:\n\n201 - unknown command\n202 - logical op"); System.out.print("erator used without values\n203 - bitwise operator used without values"); }} else { result = ""; System.out.print("Program error:\n\nUnknown"); System.out.print(" command"); System.exit(201); }} catch (Exception e) { System.out.println("Program error"); }} input.close(); }}