Advertisement
PhiNotPi

Element Interpreter in Perl

Mar 27th, 2012
995
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.57 KB | None | 0 0
  1. #Element is an interpreted programming language that I created in order to write very concise and human-readable programs for code golf competitions.
  2. #Element follows the  principle of "One character, one function, all the time." This allows the language to be much easier to learn and understand (and to write an interpreter for).
  3. #Element has three data structures: a main stack, a control stack, and a hash.
  4. #One notable feature of Element is the ability to create variables using the hash.
  5.  
  6. #This interpreter is written in Perl because I felt that most of the commands were very straight-forward to convert into Perl, and because I didn't feel like figuring out how to write it in C.
  7.  
  8. #To use, run this on the command line as "perl interpreterfilename.plx".
  9. #On the next few lines, you can type in your Element program.
  10. #Once it encounters a completely blank line, it will convert the code to Perl and print it.
  11. #It will then evaluate your program, taking input and printing output as needed.
  12.  
  13.  
  14.  
  15. $/="\n\n";
  16. chop($original=<>);
  17. $/="\n";
  18. $original=reverse$original;
  19. while($original){
  20. $char=chop$original;
  21.  
  22. $pushflag=1;
  23. $comp="";
  24.    if($char eq '_'){$comp='$a=<>;push(@m,$a);'}
  25. elsif($char eq '`'){$comp='$a=pop@m;print$a;'}
  26. elsif($char eq ';'){$comp='$a=pop@m;$b=pop@m;$hash{$a}=$b;'}
  27. elsif($char eq '~'){$comp='$a=pop@m;push(@m,$hash{$a});'}
  28. elsif($char eq ':'){$comp='$a=pop@m;$b=pop@m;for(1..$a){push(@m,$b);};'}
  29. elsif($char eq '.'){$comp='$a=pop@m;$b=pop@m;push(@m,$b.$a);'}
  30. elsif($char eq "'"){$comp='$a=pop@m;push(@c,$a);'}
  31. elsif($char eq '"'){$comp='$a=pop@c;push(@m,$a);'}
  32. elsif($char eq '$'){$comp='$a=pop@m;push(@m,length$a);'}
  33. elsif($char eq ')'){$comp='$a=pop@m;$b=chop$a;push(@m,$a);push(@m,$b);'}
  34. elsif($char eq '('){$comp='$a=pop@m;$a=reverse$a;$b=chop$a;$a=reverse$a;push(@m,$a);push(@m,$b);'}
  35. elsif($char eq '#'){$comp='$a=pop@m;'}
  36. elsif($char eq '+'){$comp='$a=pop@m;$b=pop@m;push(@m,$b+$a);'}
  37. elsif($char eq '-'){$comp='$a=pop@m;push(@m,-$a);'}
  38. elsif($char eq '*'){$comp='$a=pop@m;$b=pop@m;push(@m,$b*$a);'}
  39. elsif($char eq '/'){$comp='$a=pop@m;$b=pop@m;push(@m,$b/$a);'}
  40. elsif($char eq '%'){$comp='$a=pop@m;$b=pop@m;push(@m,$b%$a);'}
  41. elsif($char eq '^'){$comp='$a=pop@m;$b=pop@m;push(@m,$b**$a);'}
  42. elsif($char eq '?'){$comp='$a=pop@m;if($a){push(@c,1)}else{push(@c,0)};'}
  43. elsif($char eq '!'){$comp='$a=pop@c;if($a){push(@c,0)}else{push(@c,1)};'}
  44. elsif($char eq '&'){$comp='$a=pop@c;$b=pop@c;if($a && $b){push(@c,1)}else{push(@c,0)};'}
  45. elsif($char eq '|'){$comp='$a=pop@c;$b=pop@c;if($a || $b){push(@c,1)}else{push(@c,0)};'}
  46. elsif($char eq '='){$comp='$a=pop@m;$b=pop@m;if($a eq $b){push(@c,1)}else{push(@c,0)};'}
  47. elsif($char eq '>'){$comp='$a=pop@m;$b=pop@m;if($b > $a){push(@c,1)}else{push(@c,0)};'}
  48. elsif($char eq '<'){$comp='$a=pop@m;$b=pop@m;if($b < $a){push(@c,1)}else{push(@c,0)};'}
  49. elsif($char eq '['){$comp='$a=pop@c;push(@c,$a);for(1..$a){'}
  50. elsif($char eq ']'){$comp='}'}
  51. elsif($char eq '{'){$comp='while($c[$#c]){'}
  52. elsif($char eq '}'){$comp='}'}
  53. elsif($char eq ','){$comp='$a=pop@m;push(@m,chr($a));push(@m,ord($a));'}
  54. elsif($char eq '@'){$comp='$a=pop@m;$b=pop@m;for(1..$b){push(@temp,pop@m)}$c=pop@m;for(1..$b){push(@m,pop@temp)}for(1..$a){push(@temp,pop@m)}push(@m,$c);for(1..$a){push(@m,pop@temp)}'}
  55. elsif($char eq ' ' || $char eq "\n"){}
  56. else{$pushflag=0}
  57.  
  58. if($char eq '\\'){$char=chop$original}
  59.  
  60. if($pushflag==1 && $pushstring ne ""){$compiled.='push(@m,\'' . $pushstring . '\');';  $pushstring = ""}
  61. elsif($pushflag == 0){if($char eq "'" || $char eq '\\'){$char='\\'.$char}$pushstring.=$char}
  62. $compiled.=$comp;
  63. }
  64. print"$compiled\n";
  65. eval$compiled
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement