Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.FileReader;
- import java.util.LinkedList;
- import java.util.Scanner;
- public class ElogPT {
- public static void main(String[] args) {
- if(args.length == 0) {
- Scanner sc = new Scanner(System.in);
- run(sc.next(), sc.next());
- } else if(args.length == 1) run(readFile(args[0]), "");
- else if(args.length > 1) {
- String f = readFile(args[0]);
- for(int i=1;i<args.length;run(f, args[i++]));
- }
- }
- public static void run(String s, String in) {
- int cur = 0, sts = 0;
- LinkedList<Boolean> st = new LinkedList<Boolean>();
- boolean[] ins = new boolean[in.length()];
- int[] vars = new int[256];
- for(int i=0;i<256;vars[i++] = -1);
- for(int i=0;i<in.length();ins[i] = (Character.getNumericValue(in.charAt(i++)))==0?false:true);
- for(char c: s.toCharArray()) {
- if(sts == 0) {
- if(c == ';') sts = 2;
- else if(c == ':') sts = 1;
- else if(c == '.') st.push(st.peek());
- else if(c == '\'') {
- boolean b = st.pop();
- st.push(b?false:true);
- } else if(c == '&') {
- boolean b1 = st.pop(), b2 = st.pop();
- st.push(b1 && b2);
- } else if(c == '|'){
- boolean b1 = st.pop(), b2 = st.pop();
- st.push(b1 || b2);
- } else if(c == '^') {
- boolean b1 = st.pop(), b2 = st.pop();
- st.push(b1 ^ b2);
- }
- else if(c == '0') st.push(false);
- else if(c == '1') st.push(true);
- else if(c > 20){
- if(vars[c] == -1) vars[c] = (ins[cur++]?1:0);
- if(vars[c] > -1) st.push(vars[c]==1);
- }
- } else if(sts == 1 && c > 20) {
- vars[c] = (st.pop()?1:0);
- sts = 0;
- } else if(c > 20) if(c == '0' || c == '1') System.out.print(c); else System.out.print(vars[c]);
- }
- System.out.println();
- }
- public static String readFile(String filename) {
- String result = "";
- try {
- FileReader r = new FileReader(filename);
- int c;
- while((c = r.read()) != -1)
- result += (char)c;
- } catch(Exception e) {
- System.out.println(e.getMessage());
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement