comparison interps/glypho/Glypho.java @ 996:859f9b4339e6

<Gregor> tar xf egobot.tar.xz
author HackBot
date Sun, 09 Dec 2012 19:30:08 +0000
parents
children
comparison
equal deleted inserted replaced
995:6883f5911eb7 996:859f9b4339e6
1 /*
2
3 Interpreter for the Glypho language
4
5 Rune Berge 2005
6 http://rune.krokodille.com/lang
7
8 Glypho specification:
9 http://www4.ncsu.edu/~bcthomp2/glypho.txt
10
11 This software is Public Domain.
12
13 */
14
15
16 import java.io.*;
17
18 public class Glypho {
19
20 public static void displayError(String msg) {
21 System.err.println(msg);
22 System.exit(1);
23 }
24
25 public static void main(String[] args) {
26 StringBuffer source = new StringBuffer();
27 InputStream in = System.in;
28 OutputStream out = System.out;
29 OutputStream err = System.err;
30
31 if (args.length==0) displayUsage();
32
33 for (int i = 0; i < args.length; i++) {
34 if (args[i].charAt(0) == '-') {
35 switch (args[i].charAt(1)) {
36 case 'i': if ((i++) < (args.length - 1))
37 try {
38 in = new FileInputStream(args[i]);
39 }catch (FileNotFoundException e) { displayError("File not found: " + args[i]); }
40 else displayError("Missing input filename");
41 break;
42 case 'o': if ((i++) < (args.length - 1))
43 try {
44 out = new FileOutputStream(args[i]);
45 }catch (FileNotFoundException e) { displayError("Unable to write to " + args[i]); }
46 else displayError("Missing output filename");
47 break;
48 case 'e': if ((i++) < (args.length - 1))
49 try {
50 err = new FileOutputStream(args[i]);
51 }catch (FileNotFoundException e) { displayError("Unable to write to " + args[i]); }
52 else displayError("Missing error filename");
53 break;
54 case 'h': displayUsage();
55 break;
56 default: displayError("Invalid argument: " + args[i]);
57
58 }
59
60 }
61 }
62
63 if (args.length <= 0) displayError("Error: No source file specified!");
64
65 if (args.length >= 1) {
66 String sourceFile = args[args.length-1];
67
68 try {
69 FileInputStream src = new FileInputStream(sourceFile);
70 int b=-1;
71
72 b = src.read();
73 while (b>=0) {
74 source.append((char)b);
75 b = src.read();
76 }
77
78 } catch (FileNotFoundException e) { displayError("File not found: " + args[args.length-1]); }
79 catch (IOException e) { displayError(e.toString());}
80
81
82 boolean shorthand = false;
83 if (sourceFile.substring(sourceFile.length() - 4).equals(".gsh")) shorthand = true;
84
85 new Glypho(source.toString(), shorthand, in, out, err);
86
87 }
88
89 }//main()
90
91 public static void displayUsage() {
92 System.out.println("Glypho Interpreter version " + GlyphoInterpreter.VERSION);
93 System.out.println("Rune Berge - 2005 - http://rune.krokodille.com/lang");
94 System.out.println("Glypho specification: http://www4.ncsu.edu/~bcthomp2/glypho.txt");
95 System.out.println("");
96 System.out.println("This is software is Public Domain");
97 System.out.println("");
98 System.out.println("Usage: Glypho [OPTIONS] [filename]");
99 System.out.println("");
100 System.out.println("If the filename ends with .gsh it will be interpreted as Glypho shorthand.");
101 System.out.println("Otherwise it will be interpreted as regular Glypho.");
102 System.out.println("");
103 System.out.println("Options:");
104 System.out.println(" -i <filename> Read input from this file (default is standard input)");
105 System.out.println(" -o <filename> Write output to this file (default is standard output)");
106 System.out.println(" -e <filename> Write error messages to this file (default is standard output)");
107 System.out.println(" -h Display this information");
108 System.out.println("");
109 System.out.println("");
110 System.out.println("Please report any bugs to rune@krokodille.com");
111
112
113 System.exit(0);
114 }
115
116 public Glypho(String source, boolean shorthand, InputStream in, OutputStream out, OutputStream err) {
117
118 GlyphoInterpreter GI = new GlyphoInterpreter(in, out, err);
119 GI.interpret(source, shorthand);
120 }
121
122 }