Skip to content

Commit 719e8c8

Browse files
committed
integrated file read/write functionality, added tests (closes #11)
1 parent 7bde1d4 commit 719e8c8

File tree

7 files changed

+52
-33
lines changed

7 files changed

+52
-33
lines changed

PipelineScript/src/com/pipelinescript/FileManager.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,22 @@ public class FileManager
1414

1515
public static Table readTable(String file)
1616
{
17-
String path = DATA_DIR + "/" + file;
17+
String[] lines = read(file).split("\n");
1818

19-
return new Table(read(path));
19+
return new Table(lines);
2020
}
2121

2222
public static void writeTable(String file, Table data)
2323
{
2424
String path = DATA_DIR + "/" + file;
2525

26-
write(path, data.getLines());
26+
write(path, data.toString());
2727
}
2828

29-
private static String[] read(String path)
29+
public static String read(String file)
3030
{
31+
String path = DATA_DIR + "/" + file;
32+
3133
List<String> lines = new LinkedList<String>();
3234

3335
try
@@ -49,16 +51,18 @@ private static String[] read(String path)
4951
e.printStackTrace();
5052
}
5153

52-
return lines.toArray(new String[]{});
54+
return String.join("\n", lines);
5355
}
5456

55-
private static void write(String path, String[] data)
57+
public static void write(String file, String data)
5658
{
59+
String path = DATA_DIR + "/" + file;
60+
5761
try
5862
{
5963
PrintWriter writer = new PrintWriter(new FileWriter(path));
6064

61-
for(String line : data)
65+
for(String line : data.split("\n"))
6266
{
6367
writer.println(line);
6468
}

data/bar.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
foo

doc/examples.md

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -278,45 +278,43 @@ Note: The first argument will be the name of the pipeline file, like in Shell sc
278278

279279
PipelineScript
280280
```
281-
{{1, 2}, {3, 4}} > "foobar.csv"
282-
table t = @"foobar.csv"
283-
print t
281+
"foo" -> "bar.txt"
282+
text x = @"bar.txt"
283+
print x
284284
```
285285

286286
Java
287287
```java
288-
FileManager.writeTable("foobar.csv", new Table(new String[][]{{"1", "2"}, {"3", "4"}}));
289-
Table t = FileManager.readTable("foobar.csv");
290-
System.out.println(t);
288+
FileManager.write("bar.txt", "foo");
289+
String x = FileManager.read("bar.txt");
290+
System.out.println(x);
291291
```
292292

293293
Output
294294
```
295-
1,2
296-
3,4
295+
foo
297296
```
298297

299298

300299
###file_write
301300

302301
PipelineScript
303302
```
304-
table t = {{1, 2}, {3, 4}}
305-
t > "foobar.csv"
306-
print @"foobar.csv"
303+
text x = "foo"
304+
x -> "bar.txt"
305+
print @"bar.txt"
307306
```
308307

309308
Java
310309
```java
311-
Table t = new Table(new String[][]{{"1", "2"}, {"3", "4"}});
312-
FileManager.writeTable("foobar.csv", t);
313-
System.out.println(FileManager.readTable("foobar.csv");
310+
String x = "foo";
311+
FileManager.write("bar.txt", x);
312+
System.out.println(FileManager.read("bar.txt"));
314313
```
315314

316315
Output
317316
```
318-
1,2
319-
3,4
317+
foo
320318
```
321319

322320

@@ -344,5 +342,3 @@ PERSON President Obama
344342
PERSON Putin
345343
LOCATION Geneva
346344
```
347-
348-

pls.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
'LT', 'GT', 'LEQ', 'GEQ', 'DE', 'NE',
1717
'AND', 'OR', 'NOT',
1818
'NUM', 'TEXT', 'TABLE', 'DOT', 'GRAPH',
19-
'DOLLAR'
19+
'DOLLAR', 'FREAD', 'FWRITE'
2020
)
2121

2222
# operator precedence
@@ -51,6 +51,8 @@
5151
t_NAME = r'[a-zA-Z_][a-zA-Z0-9_]*'
5252
t_DOT = r'\.'
5353
t_DOLLAR = r'\$'
54+
t_FREAD = r'\@'
55+
t_FWRITE = r'->'
5456

5557
# ignored characters
5658
t_ignore = " \t"
@@ -484,12 +486,20 @@ def p_entry2(t):
484486
t[0] = "{%s}"%t[2]
485487

486488
######################### SHELL_ARGS
487-
489+
488490
def p_shell_args(t):
489491
'value : DOLLAR value'
490492
t[0] = "args[(int)%s]"%t[2]
491-
492493

494+
######################### FILE_IO
495+
496+
def p_file_read(t):
497+
'value : FREAD value'
498+
t[0] = "FileManager.read(%s)"%t[2]
499+
500+
def p_file_write(t):
501+
'statement : value FWRITE value'
502+
t[0] = "FileManager.write(%s, %s);"%(t[3], t[1])
493503

494504

495505

@@ -542,7 +552,8 @@ def get_indexed_array_type(x):
542552

543553

544554
print header
545-
print "public class Output\n{"
555+
print "package com.pipelinescript;"
556+
print "public class Pipeline\n{"
546557
print functions
547558
print "public static void main(String[] args) throws Exception\n{"
548559
print body

pls.sh

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
python pls.py $1 > Output.java 2> /dev/null
2-
javac Output.java 2> /dev/null
3-
java -cp . Output $*
4-
rm Output.java Output.class parser.out parsetab.py parsetab.pyc 2> /dev/null
1+
python pls.py $1 > PipelineScript/src/com/pipelinescript/Pipeline.java 2> /dev/null
2+
javac -d PipelineScript/bin PipelineScript/src/com/pipelinescript/*.java 2> /dev/null
3+
java -cp PipelineScript/bin com.pipelinescript.Pipeline
4+
rm PipelineScript/src/com/pipelinescript/Pipeline.java
5+
rm parser.out parsetab.py parsetab.pyc 2> /dev/null

tests/file_read.pls

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"foo" -> "bar.txt"
2+
text x = @"bar.txt"
3+
print x

tests/file_write.pls

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
text x = "foo"
2+
x -> "bar.txt"
3+
print @"bar.txt"

0 commit comments

Comments
 (0)