Permalink
Browse files

Work from CSC 430, Programming Languages 1.

  • Loading branch information...
0 parents commit 36d8073765cb75cd4a059af1b53d990afa8b0c94 @e28eta committed May 10, 2012
Showing 891 changed files with 17,343 additions and 0 deletions.
@@ -0,0 +1,106 @@
+(* Daniel Jackson *)
+(* 430 - HW 1 *)
+
+(* Part 1 *)
+fun number_of v [] = 0
+ | number_of v (x::xs) =
+ (if v=x then 1 else 0) + (number_of v xs);
+
+
+(* Part 2 *)
+fun pair_swap [] = []
+ | pair_swap ((a,b)::xs) = (b, a)::(pair_swap xs);
+
+
+(* Part 3 *)
+exception ImbalancedWeaving;
+
+fun weave [] [] = []
+ | weave (x::[]) [] = (x::[])
+ | weave [] (x::xs) = raise ImbalancedWeaving
+ | weave (x::xs) [] = raise ImbalancedWeaving
+ | weave (x::xs) (y::ys) = (x::(y::(weave xs ys)));
+
+
+(* Part 4 *)
+fun char_to_print c [] = c
+ | char_to_print c ((x,y)::tuples) =
+ if (c=x) then y else char_to_print c tuples;
+
+fun file_subst1 input list =
+ let
+ val c = TextIO.input1(input);
+ in
+ if(TextIO.endOfStream input)
+ then ()
+ else (print (str (char_to_print (valOf c) list));
+ file_subst1 input list)
+ end;
+
+fun file_subst filename list =
+ file_subst1 (TextIO.openIn filename) list;
+
+(* Part 5 *)
+datatype 'a ThingCollection =
+OneThing of ('a * 'a ThingCollection)
+| TwoThings of ('a * 'a * 'a ThingCollection)
+| ManyThings of ('a list * 'a ThingCollection)
+| Nothing
+;
+
+fun sum_list [] = 0
+ | sum_list (x::xs) = 1 + sum_list xs;
+
+fun number_of_things coll =
+ case coll of
+ Nothing => 0
+ | (OneThing (thing, collection)) => 1 + number_of_things collection
+ | (TwoThings (thing1, thing2, collection)) => 2 + (number_of_things collection)
+ | (ManyThings (thingList, collection)) =>
+ (sum_list thingList) + (number_of_things collection);
+
+(* Part 6 *)
+fun number_of_OneThing coll =
+ case coll of
+ Nothing => 0
+ | (OneThing (thing, collection)) => 1 + number_of_OneThing collection
+ | (TwoThings (thing1, thing2, collection)) => 0 + (number_of_OneThing collection)
+ | (ManyThings (thingList, collection)) => 0 + (number_of_OneThing collection);
+
+(* Part 7 *)
+fun number_of_XThing test coll =
+ case coll of
+ Nothing => if(test Nothing) then 1 else 0
+ | (OneThing (thing, collection)) =>
+ (if(test coll) then 1 else 0) + number_of_XThing test collection
+ | (TwoThings (thing1, thing2, collection)) =>
+ (if(test coll) then 1 else 0) + (number_of_XThing test collection)
+ | (ManyThings (thingList, collection)) =>
+ (if(test coll) then 1 else 0) + (number_of_XThing test collection);
+
+(* Part 8 *)
+fun number_of_TwoThings collection = number_of_XThing (fn (TwoThings _) => true | _ => false) collection;
+
+
+(* Part 9 *)
+fun map_thing_collection func coll =
+ case coll of
+ Nothing => Nothing
+ | (OneThing (thing, collection)) => OneThing (func thing,
+ map_thing_collection func collection)
+ | (TwoThings (thing1, thing2, collection)) => TwoThings (func thing1,
+ func thing2, map_thing_collection func collection)
+ | (ManyThings (thingList, collection)) => ManyThings (map func
+ thingList, map_thing_collection func collection);
+
+
+(* Part 10 *)
+fun extract_values coll =
+ case coll of
+ Nothing => []
+ | (OneThing (thing, collection)) => thing::(extract_values collection)
+ | (TwoThings (thing1, thing2, collection)) => thing1::thing2::(extract_values collection)
+ | (ManyThings (thingList, collection)) => thingList @ (extract_values collection);
+
+fun flatten_collection collection =
+ ManyThings (extract_values collection, Nothing);
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,123 @@
+(* Daniel Jackson *)
+(* 430 - HW 2 *)
+
+(* Part 1 - Recognizer *)
+(* Builds the text of the identifier, so it can be checked for keyword *)
+fun buildIdent theInstream =
+ if ((isSome (TextIO.lookahead theInstream))
+ andalso (Char.isAlphaNum (valOf (TextIO.lookahead theInstream))))
+ then
+ (str (valOf (TextIO.input1 theInstream))) ^ (buildIdent theInstream)
+ else
+ "";
+
+(* returns true if it is a keyword, false if not *)
+fun isKeyword token =
+ if (token = "int") then true
+ else if (token = "bool") then true
+ else if (token = "fn") then true
+ else if (token = "write") then true
+ else if (token = "writeline") then true
+ else if (token = "if") then true
+ else if (token = "else") then true
+ else if (token = "while") then true
+ else if (token = "true") then true
+ else if (token = "false") then true
+ else if (token = "return") then true
+ else if (token = "var") then true
+ else if (token = "unit") then true
+ else false;
+
+(* Prints the identifier/keyword with a prefix *)
+fun recognizeIdent theInstream =
+ let
+ val token = buildIdent theInstream
+ in
+ if (isKeyword token)
+ then print ("keyword: " ^ token ^ "\n")
+ else print ("identifier: " ^ token ^ "\n")
+ end;
+
+(* Builds a string containing the text of the number *)
+fun buildNumber theInstream =
+ if ((isSome (TextIO.lookahead theInstream))
+ andalso (Char.isDigit (valOf (TextIO.lookahead theInstream))))
+ then
+ (str (valOf (TextIO.input1 theInstream))) ^ (buildNumber theInstream)
+ else
+ "";
+
+(* Prints the number with a prefix *)
+fun recognizeNumber theInstream =
+ print ("number: " ^ (buildNumber theInstream) ^ "\n");
+
+fun buildAssign theInstream =
+ if ((isSome (TextIO.lookahead theInstream))
+ andalso (valOf(TextIO.input1 theInstream) = #"="))
+ then print ("symbol: :=\n")
+ else OS.Process.exit OS.Process.failure;
+
+fun buildExclaim theInstream =
+ if ((isSome (TextIO.lookahead theInstream))
+ andalso (valOf(TextIO.lookahead theInstream) = #"="))
+ then (TextIO.input1 theInstream; print ("symbol: !=\n"))
+ else print("symbol: !\n");
+
+fun buildMinus theInstream =
+ if ((isSome (TextIO.lookahead theInstream))
+ andalso (valOf(TextIO.lookahead theInstream) = #">"))
+ then (TextIO.input1 theInstream; print ("symbol: ->\n"))
+ else print("symbol: -\n");
+
+fun buildLT theInstream =
+ if ((isSome (TextIO.lookahead theInstream))
+ andalso (valOf(TextIO.lookahead theInstream) = #"="))
+ then (TextIO.input1 theInstream; print ("symbol: <=\n"))
+ else print("symbol: <\n");
+
+fun buildGT theInstream =
+ if ((isSome (TextIO.lookahead theInstream))
+ andalso (valOf(TextIO.lookahead theInstream) = #"="))
+ then (TextIO.input1 theInstream; print ("symbol: >=\n"))
+ else print("symbol: >\n");
+
+(* Prints the symbol with a prefix *)
+fun recognizePunct theInstream =
+ case valOf (TextIO.input1 theInstream) of
+ #":" => buildAssign theInstream
+ | #"{" => print ("symbol: {\n")
+ | #"}" => print ("symbol: }\n")
+ | #"(" => print ("symbol: (\n")
+ | #")" => print ("symbol: )\n")
+ | #"," => print ("symbol: ,\n")
+ | #";" => print ("symbol: ;\n")
+ | #"&" => print ("symbol: &\n")
+ | #"|" => print ("symbol: |\n")
+ | #"=" => print ("symbol: =\n")
+ | #"+" => print ("symbol: +\n")
+ | #"*" => print ("symbol: *\n")
+ | #"/" => print ("symbol: /\n")
+ | #"!" => buildExclaim theInstream
+ | #"-" => buildMinus theInstream
+ | #"<" => buildLT theInstream
+ | #">" => buildGT theInstream
+ | _ => OS.Process.exit OS.Process.failure;
+
+(* recognizeToken function *)
+fun recognizeToken theInstream =
+ case (TextIO.lookahead theInstream) of
+ (SOME char) =>
+ if (Char.isAlpha char)
+ then
+ recognizeIdent theInstream
+ else if (Char.isDigit char)
+ then recognizeNumber theInstream
+ else if (Char.isPunct char)
+ then recognizePunct theInstream
+ else (* Non-graphical, ignore it. Means the call
+ stack will have as many frames as the
+ stream has non-graphical chars *)
+ (TextIO.input1 theInstream;
+ recognizeToken theInstream)
+ | NONE => print ("end-of-file\n")
+;
@@ -0,0 +1,24 @@
+#!/bin/csh -f
+# test one and do diff
+
+set b = `basename ${2} .df`
+set dir = `dirname ${2}`
+set outdir = ${3}
+
+sml >&! ${outdir}/${b}.tmp << END
+val devnull = Posix.FileSys.openf ("/dev/null", Posix.FileSys.O_WRONLY, Posix.FileSys.O.trunc);
+val sout = Posix.IO.dup(Posix.FileSys.stdout);
+Posix.IO.dup2 {old=devnull, new=Posix.FileSys.stdout};
+use "${1}";
+Posix.IO.dup2 {old=sout, new=Posix.FileSys.stdout};
+fun untilEOF f fstr =
+ if TextIO.endOfStream fstr
+ then ()
+ else (f fstr; untilEOF f fstr);
+untilEOF recognizeToken (TextIO.openIn "${dir}/${b}");
+END
+grep -v "GC #" ${outdir}/${b}.tmp | grep -v "Standard ML" | grep -v "autoloading" | grep -v "val " | grep -v "basis" | grep -v '^- $' >&! ${outdir}/${b}.output
+rm ${outdir}/${b}.tmp
+diff ${outdir}/${b}.correct ${outdir}/${b}.output
+rm ${outdir}/${b}.output
+
@@ -0,0 +1,35 @@
+#!/bin/csh -f
+# test one and do diff
+
+set b = `basename ${2} .df`
+set dir = `dirname ${2}`
+set outdir = ${3}
+
+sml >&! ${3}/${b}.tmp << END
+val devnull = Posix.FileSys.openf ("/dev/null", Posix.FileSys.O_WRONLY, Posix.FileSys.O.trunc);
+val sout = Posix.IO.dup(Posix.FileSys.stdout);
+Posix.IO.dup2 {old=devnull, new=Posix.FileSys.stdout};
+use "${1}";
+val fstr = TextIO.openIn "${dir}/${b}";
+Posix.IO.dup2 {old=sout, new=Posix.FileSys.stdout};
+nextToken fstr;
+nextToken fstr;
+nextToken fstr;
+nextToken fstr;
+nextToken fstr;
+nextToken fstr;
+nextToken fstr;
+nextToken fstr;
+nextToken fstr;
+nextToken fstr;
+nextToken fstr;
+nextToken fstr;
+nextToken fstr;
+nextToken fstr;
+nextToken fstr;
+END
+grep -v "GC #" ${outdir}/${b}.tmp | grep -v "Standard ML" | grep -v "autoloading" | grep -v "basis" | grep -v '^- $' >&! ${outdir}/${b}.output
+rm ${outdir}/${b}.tmp
+diff ${outdir}/${b}.correct ${outdir}/${b}.output
+rm ${outdir}/${b}.output
+
@@ -0,0 +1,17 @@
+#!/bin/csh -f
+# a simple way to test everything
+
+if (${1} == "" || ${2} == "") then
+echo "must provide source files (recognizer and tokenizer)"; exit 1
+endif
+
+foreach i ( tests/input/* )
+ echo ==== Part 1 -- $i ====
+ ./run_rec ${1} $i tests/1_recognizer
+end
+
+foreach i ( tests/input/* )
+ echo ==== Part2 -- $i ====
+ ./run_tok ${2} $i tests/2_tokenizer
+end
+
@@ -0,0 +1,3 @@
+symbol: {
+symbol: }
+end-of-file
@@ -0,0 +1,7 @@
+keyword: var
+keyword: int
+identifier: i
+symbol: ;
+symbol: {
+symbol: }
+end-of-file
@@ -0,0 +1,11 @@
+keyword: var
+keyword: int
+identifier: i
+symbol: ,
+identifier: j
+symbol: ,
+identifier: k
+symbol: ;
+symbol: {
+symbol: }
+end-of-file
@@ -0,0 +1,28 @@
+keyword: var
+keyword: int
+identifier: i
+symbol: ,
+identifier: j
+symbol: ;
+keyword: var
+keyword: fn
+keyword: int
+symbol: ->
+keyword: int
+identifier: k
+symbol: ;
+keyword: var
+keyword: bool
+identifier: b
+symbol: ,
+identifier: p
+symbol: ,
+identifier: q
+symbol: ;
+keyword: var
+keyword: int
+identifier: m
+symbol: ;
+symbol: {
+symbol: }
+end-of-file
@@ -0,0 +1,15 @@
+keyword: var
+keyword: int
+identifier: i
+symbol: ;
+keyword: var
+keyword: bool
+identifier: l
+symbol: ;
+symbol: {
+identifier: i
+symbol: :=
+number: 1
+symbol: ;
+symbol: }
+end-of-file
@@ -0,0 +1,19 @@
+keyword: var
+keyword: bool
+identifier: i
+symbol: ;
+keyword: var
+keyword: bool
+identifier: b
+symbol: ;
+symbol: {
+identifier: i
+symbol: :=
+identifier: a
+symbol: ;
+identifier: b
+symbol: :=
+identifier: b
+symbol: ;
+symbol: }
+end-of-file
Oops, something went wrong.

0 comments on commit 36d8073

Please sign in to comment.