#!/usr/bin/perl -w # Author: Paul Fitzpatrick, paulfitz@ai.mit.edu # Copyright (c) 2003 Paul Fitzpatrick # # This file is part of CosmicOS. # # CosmicOS is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # CosmicOS is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with CosmicOS; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA use strict; my %token; # library of tokens (symbol sequences) used for designer's convenience. # make sure names are uniquely decodable, or put in spaces! $token{"."} = "h\n"; # end of line $token{" "} = ""; # whitespace is ignored - no translation $token{"\n"} = ""; $token{"\r"} = ""; $token{"\t"} = ""; $token{"0"} = "a"; # zero $token{","} = "b"; # increment $token{":"} = "c"; # double $token{"<"} = "daa"; # less than $token{"="} = "dac"; # equal to $token{">"} = "dae"; # greater than $token{"and"} = "dba"; # logical and $token{"or"} = "dbb"; # logical or $token{"not"} = "dbc"; # logical not $token{"+"} = "dca"; # addition $token{"*"} = "dcb"; # multiplication $token{"set"} = "dda"; # store in register (push-like) $token{"get"} = "ddb"; # read from register $token{"nu"} = "ddc"; # make new instance of register $token{"un"} = "ddd"; # destroy most recent instance of register $token{"?"} = "dea"; # unknown value, filled in later $token{"fx"} = "deb"; # make into a function $token{"!"} = "def"; # fill in last unknown value and evaluate $token{"if"} = "dfa"; # control flow --- if $token{"false"} = "ea";# logical false $token{"true"} = "eb"; # logical true $token{"ramble"} = "fff"; # sentence-has-no-defined-truth-value sub irand { return int(rand(shift)); }; sub EvalCosmic { # simple test procedure to make sure assertions are at least true my $txt = shift; my @cmds = @_; my @stk = (); my @result = (); my $line = 0; my @lines = split(/\n/,$txt); my %registers; foreach my $cmd (@cmds) { SWITCH : { if ($cmd =~ /\[([0-9]+)\]/) { $line = $1; } if ($cmd eq "0") { push(@stk,0); } if ($cmd eq ",") { push(@stk,pop(@stk)+1); } if ($cmd eq ":") { push(@stk,pop(@stk)*2); } if ($cmd eq "not") { push(@stk,pop(@stk)==0); } if ($cmd eq "true") { push(@stk,1); } if ($cmd eq "false") { push(@stk,0); } if ($cmd eq "set") { my $v2 = pop(@stk); my $v1 = pop(@stk); if (!defined($registers{$v2})) { $registers{$v2} = [ 0 ]; } pop(@{$registers{$v2}}); push(@{$registers{$v2}},$v1); } if ($cmd eq "get") { my $v2 = pop(@stk); my $v1 = 0; if (!defined($registers{$v2})) { $registers{$v2} = [ 0 ]; } $v1 = pop(@{$registers{$v2}}); push(@{$registers{$v2}},$v1); push(@stk,$v1); } if ($cmd eq "nu") { my $v2 = pop(@stk); if (!defined($registers{$v2})) { $registers{$v2} = [ 0 ]; } push(@{$registers{$v2}},0); } if ($cmd eq "un") { my $v2 = pop(@stk); if (!defined($registers{$v2})) { $registers{$v2} = [ 0 ]; } pop(@{$registers{$v2}}); } if ($cmd eq "+") { my $v2 = pop(@stk); my $v1 = pop(@stk); if (!defined($v2)) { die "argument $lines[$line]\n" }; if (!defined($v1)) { die "argument $lines[$line]\n" }; push(@stk,0+($v1+$v2)); } if ($cmd eq "?") { push(@stk,0); } if ($cmd eq "*") { my $v2 = pop(@stk); my $v1 = pop(@stk); if (!defined($v2)) { die "argument $lines[$line]\n" }; if (!defined($v1)) { die "argument $lines[$line]\n" }; push(@stk,0+($v1*$v2)); } if ($cmd eq ">") { my $v2 = pop(@stk); my $v1 = pop(@stk); if (!defined($v2)) { die "argument $lines[$line]\n" }; if (!defined($v1)) { die "argument $lines[$line]\n" }; push(@stk,0+($v1>$v2)); } if ($cmd eq "<") { my $v2 = pop(@stk); my $v1 = pop(@stk); if (!defined($v2)) { die "argument $lines[$line]\n" }; if (!defined($v1)) { die "argument $lines[$line]\n" }; push(@stk,0+($v1<$v2)); } if ($cmd eq "=") { my $v2 = pop(@stk); my $v1 = pop(@stk); if (!defined($v2)) { die "argument $lines[$line]\n" }; if (!defined($v1)) { die "argument $lines[$line]\n" }; push(@stk,0+($v1==$v2)); } if ($cmd eq "and") { my $v2 = pop(@stk); my $v1 = pop(@stk); if (!defined($v2)) { die "argument $lines[$line]\n" }; if (!defined($v1)) { die "argument $lines[$line]\n" }; push(@stk,0+($v1&&$v2)); } if ($cmd eq "or") { my $v2 = pop(@stk); my $v1 = pop(@stk); if (!defined($v2)) { die "argument $lines[$line]\n" }; if (!defined($v1)) { die "argument $lines[$line]\n" }; push(@stk,0+($v1||$v2)); } if ($cmd eq ".") { my $v = pop(@stk); if (!defined($v)) { $v = 0; }; if ($v == 0) { my $info = "$v for line $line //"; $info .= $lines[$line]; push(@result,$info); } @stk = (); } } } print "RESULTS:\n", join("\n",@result), "\n"; }; sub Tokens2Msg { my $msg = ""; foreach my $t (@_) { if (!($t =~ /\[/)) { $msg .= $token{$t}; } } return $msg; }; sub Text2Tokens { my $txt = shift; my $base = 0; my @tokens = (); $txt =~ s/\#.*//g; #$txt =~ s/\[[^\]]*\]//g; while ($txt =~ /\[([^\]\+\-]*)\]/) { my $n = $1; my $t = ShowBinaryVerbose($n); $txt =~ s/\[$n\]/$t/g; } my $first_high = 42; my %translate_high; while ($txt =~ /(\{[^\}\+\-]*\})/) { my $x = $1; my $qx = quotemeta($x); if (!defined($translate_high{$x})) { $translate_high{$x} = $first_high; $first_high++; } my $qy = $translate_high{$x}; $txt =~ s/$qx/$qy/g; } $txt =~ s/\{[^\}]*\}//g; my $line = 0; for (my $i=0; $i0) { my $p = int(log($val)/log(2)); for (my $j=$p; $j>=0; $j--) { if ($j<$p) { $txt .= ":"; } if ($val>=(2**$j)) { $txt .= ","; $val -= (2**$j); } } } # $txt .= "[$i]"; return $txt; }; sub ShowUnaryLesson { my $txt = ""; $txt .= "# introduce counting\n"; #$txt .= "{+UnaryLesson}\n"; for (my $i=0; $i<=5; $i++) { $txt .= ShowUnary($i); } $txt .= ".\n"; for (my $i=5; $i>=0; $i--) { $txt .= ShowUnary($i); } $txt .= ".\n"; $txt .= "# now show equality\n"; for (my $i=0; $i<=5; $i++) { my $r = irand(6); $txt .= ShowUnary($r); $txt .= ShowUnary($r); $txt .= "=.\n"; } $txt .= "# now show other comparisons\n"; for (my $i=0; $i<=10; $i++) { my $r = irand(6); my $r2 = irand($r); $txt .= ShowUnary($r+1); $txt .= ShowUnary($r2); $txt .= ">.\n"; } for (my $i=0; $i<=10; $i++) { my $r = irand(6); my $r2 = irand($r); $txt .= ShowUnary($r2); $txt .= ShowUnary($r+1); $txt .= "<.\n"; } for (my $i=0; $i<=10; $i++) { my $r = irand(6); my $r2 = irand(6); $txt .= ShowUnary($r); $txt .= ShowUnary($r2); if ($r>$r2) { $txt .= ">"; } elsif ($r<$r2) { $txt .= "<"; } else { $txt .= "="; } $txt .= ".\n"; } #$txt .= "{-UnaryLesson}\n"; return $txt; }; sub ShowNotLogicLesson { my $txt = ""; $txt .= "# introduce the NOT logical operator\n"; for (my $i=0; $i<=5; $i++) { my $r = irand(6); $txt .= ShowUnary($r); $txt .= ShowUnary($r); $txt .= "=.\n"; $txt .= ShowUnary($r); $txt .= ShowUnary($r); $txt .= "< not.\n"; $txt .= ShowUnary($r); $txt .= ShowUnary($r); $txt .= "> not.\n"; } for (my $i=0; $i<=5; $i++) { my $r = irand(6); my $r2 = $r+1+irand(3); $txt .= ShowUnary($r); $txt .= ShowUnary($r2); $txt .= "= not.\n"; $txt .= ShowUnary($r); $txt .= ShowUnary($r2); $txt .= "<.\n"; $txt .= ShowUnary($r); $txt .= ShowUnary($r2); $txt .= "> not.\n"; } for (my $i=0; $i<=5; $i++) { my $r = irand(6); my $r2 = $r+1+irand(3); $txt .= ShowUnary($r2); $txt .= ShowUnary($r); $txt .= "= not.\n"; $txt .= ShowUnary($r2); $txt .= ShowUnary($r); $txt .= "< not.\n"; $txt .= ShowUnary($r2); $txt .= ShowUnary($r); $txt .= ">.\n"; } return $txt; }; sub ShowTrueComparison { my $txt = ""; my $c = irand(3); if ($c==0) { my $r = irand(6); $txt .= ShowUnary($r); $txt .= ShowUnary($r); $txt .= "="; } elsif ($c==1) { my $r = irand(6); my $r2 = $r+1+irand(3); $txt .= ShowUnary($r); $txt .= ShowUnary($r2); $txt .= "<"; } else { my $r = irand(6); my $r2 = $r+1+irand(3); $txt .= ShowUnary($r2); $txt .= ShowUnary($r); $txt .= ">"; } return $txt; } sub ShowFalseComparison { my $txt = ""; my $c = irand(3); if ($c==0) { my $r = irand(6); my $r2 = irand(6); if ($r == $r2) { if(irand(2)==1) { $r++; } else { $r2++; } } $txt .= ShowUnary($r); $txt .= ShowUnary($r2); $txt .= "="; } else { my $r = irand(7); my $r2 = irand(7); $txt .= ShowUnary($r); $txt .= ShowUnary($r2); if ($r>$r2) { $txt .= "<"; } else { $txt .= ">"; } } return $txt; } sub ShowAndLogicLesson { my $txt = ""; $txt .= "# introduce the AND logical operator\n"; for (my $i=0; $i<10; $i++) { $txt .= ShowTrueComparison(); $txt .= " "; $txt .= ShowTrueComparison(); $txt .= " and.\n"; } for (my $i=0; $i<5; $i++) { $txt .= ShowTrueComparison(); $txt .= " "; $txt .= ShowFalseComparison(); $txt .= " and not.\n"; } for (my $i=0; $i<5; $i++) { $txt .= ShowFalseComparison(); $txt .= " "; $txt .= ShowTrueComparison(); $txt .= " and not.\n"; } for (my $i=0; $i<5; $i++) { $txt .= ShowFalseComparison(); $txt .= " "; $txt .= ShowFalseComparison(); $txt .= " and not.\n"; } for (my $i=0; $i<10; $i++) { my $t1 = irand(2); my $t2 = irand(2); if ($t1==1) { $txt .= ShowTrueComparison(); } else { $txt .= ShowFalseComparison(); } $txt .= " "; if ($t2==1) { $txt .= ShowTrueComparison(); } else { $txt .= ShowFalseComparison(); } $txt .= " and"; if (!(($t1==1)&&($t2==1))) { $txt .= " not"; } $txt .= ".\n"; } return $txt; } sub ShowOrLogicLesson { my $txt = ""; $txt .= "# introduce the OR logical operator\n"; for (my $i=0; $i<5; $i++) { $txt .= ShowTrueComparison(); $txt .= " "; $txt .= ShowFalseComparison(); $txt .= " or.\n"; } for (my $i=0; $i<5; $i++) { $txt .= ShowFalseComparison(); $txt .= " "; $txt .= ShowTrueComparison(); $txt .= " or.\n"; } for (my $i=0; $i<5; $i++) { $txt .= ShowTrueComparison(); $txt .= " "; $txt .= ShowTrueComparison(); $txt .= " or.\n"; } for (my $i=0; $i<5; $i++) { $txt .= ShowFalseComparison(); $txt .= " "; $txt .= ShowFalseComparison(); $txt .= " or not.\n"; } for (my $i=0; $i<10; $i++) { my $t1 = irand(2); my $t2 = irand(2); if ($t1==1) { $txt .= ShowTrueComparison(); } else { $txt .= ShowFalseComparison(); } $txt .= " "; if ($t2==1) { $txt .= ShowTrueComparison(); } else { $txt .= ShowFalseComparison(); } $txt .= " or"; if (!(($t1==1)||($t2==1))) { $txt .= " not"; } $txt .= ".\n"; } return $txt; } sub ShowTrueFalseLesson { my $txt = ""; $txt .= "# use equality for truth values\n"; for (my $i=0; $i<5; $i++) { $txt .= ShowTrueComparison(); $txt .= ShowTrueComparison(); $txt .= " =.\n"; } for (my $i=0; $i<5; $i++) { $txt .= ShowFalseComparison(); $txt .= ShowFalseComparison(); $txt .= " =.\n"; } for (my $i=0; $i<5; $i++) { $txt .= ShowFalseComparison(); $txt .= ShowTrueComparison(); $txt .= " = not.\n"; } for (my $i=0; $i<5; $i++) { $txt .= ShowTrueComparison(); $txt .= ShowFalseComparison(); $txt .= " = not.\n"; } for (my $i=0; $i<5; $i++) { $txt .= "true "; $txt .= ShowTrueComparison(); $txt .= " =.\n"; } for (my $i=0; $i<5; $i++) { $txt .= ShowTrueComparison(); $txt .= " true"; $txt .= " =.\n"; } for (my $i=0; $i<5; $i++) { $txt .= "false "; $txt .= ShowFalseComparison(); $txt .= " =.\n"; } for (my $i=0; $i<5; $i++) { $txt .= ShowFalseComparison(); $txt .= " false"; $txt .= " =.\n"; } $txt .= "true true =.\n"; $txt .= "false false =.\n"; $txt .= "true false = not.\n"; $txt .= "false true = not.\n"; return $txt; }; sub ShowAdditionLesson { my $txt = ""; $txt .= "# introduce addition\n"; for (my $i=0; $i<10; $i++) { my $r = irand(5); my $r2 = irand(5); $txt .= ShowUnary($r); $txt .= ShowUnary($r2); $txt .= "+"; $txt .= ShowUnary($r+$r2); $txt .= "=.\n"; } return $txt; }; sub ShowMultiplicationLesson { my $txt = ""; $txt .= "# introduce multiplication\n"; for (my $i=0; $i<=3; $i++) { for (my $j=0; $j<=3; $j++) { $txt .= ShowUnary($i); $txt .= ShowUnary($j); $txt .= "*"; $txt .= ShowUnary($i*$j); $txt .= "=.\n"; } } for (my $i=0; $i<10; $i++) { my $r = irand(4); my $r2 = irand(4); $txt .= ShowUnary($r); $txt .= ShowUnary($r2); $txt .= "*"; $txt .= ShowUnary($r*$r2); $txt .= "=.\n"; } return $txt; }; sub ShowDoubleLesson { my $txt = ""; $txt .= "# introduce doubling as a special case of multiplication\n"; $txt .= "# as prelude to binary representation\n"; for (my $i=0; $i<=4; $i++) { $txt .= ShowUnary($i); $txt .= ":"; $txt .= ShowUnary($i*2); $txt .= "=.\n"; } for (my $i=0; $i<=4; $i++) { $txt .= ShowUnary($i*2); $txt .= ShowUnary($i); $txt .= ":"; $txt .= "=.\n"; } for (my $i=0; $i<=4; $i++) { $txt .= ShowUnary($i); $txt .= ShowUnary(2); $txt .= "*"; $txt .= ShowUnary($i); $txt .= ":"; $txt .= "=.\n"; } return $txt; }; sub ShowBinaryLesson { my $txt = ""; $txt .= "# introduce a simple form of binary notation\n"; $txt .= "# (a previous lesson reinforcing how operators chain would help)\n"; $txt .= "# essentially just use fact that '0' puts zero on stack, ',' increments it,\n"; $txt .= "# and ':' doubles it.\n"; $txt .= "# After this lesson, in the higher-level version of the message,\n"; $txt .= "# will use [XX] to stand for the binary notation given.\n"; for (my $i=0; $i<16; $i++) { $txt .= ShowUnary($i); $txt .= ShowBinaryVerbose($i); $txt .= "=.\n"; } for (my $i=0; $i<16; $i++) { my $j = irand(16); $txt .= ShowBinaryVerbose($j); $txt .= ShowUnary($j); $txt .= "=.\n"; } for (my $i=0; $i<8; $i++) { my $r = irand(16); my $r2 = irand(16); $txt .= ShowBinaryVerbose($r); $txt .= ShowBinaryVerbose($r2); $txt .= "+"; $txt .= ShowBinaryVerbose($r+$r2); $txt .= "=.\n"; } for (my $i=0; $i<8; $i++) { my $r = irand(16); my $r2 = irand(16); $txt .= ShowBinaryVerbose($r); $txt .= ShowBinaryVerbose($r2); $txt .= "*"; $txt .= ShowBinaryVerbose($r*$r2); $txt .= "=.\n"; } return $txt; }; sub ShowEvaluationLesson { my $txt = ""; $txt .= "# demonstrate idea of leaving gaps in an expression\n"; $txt .= "# and then filling them in afterwards\n"; for (my $i=0; $i<8; $i++) { my $r = irand(16); my $r2 = irand(16); $txt .= ShowBinary($r); $txt .= "?"; $txt .= "+"; $txt .= ShowBinary($r+$r2); $txt .= "="; $txt .= ShowBinary($r2); $txt .= "!\n"; } for (my $i=0; $i<8; $i++) { my $r = irand(16); my $r2 = irand(16); $txt .= "??"; $txt .= "*"; $txt .= ShowBinary($r*$r2); $txt .= "="; $txt .= ShowBinary($r); $txt .= "!"; $txt .= ShowBinary($r2); $txt .= "!\n"; } $txt .= "# this example is important since it suggests the ordering on '!'\n"; for (my $i=0; $i<8; $i++) { my $r = irand(16); my $r2 = irand(16); $txt .= "??"; $txt .= "+"; $txt .= "?="; $txt .= ShowBinary($r+$r2); $txt .= "!"; $txt .= ShowBinary($r2); $txt .= "!"; $txt .= ShowBinary($r); $txt .= "!\n"; } return $txt; }; sub ShowSetGetLesson { my $txt = ""; $txt .= "# start to illustrate the semantics of registers\n"; for (my $i=0; $i<12; $i++) { my $r = irand(16); my $r2 = irand(4); my $r3 = irand(16); my $r4 = irand(4); if ($r3==$r) { $r++; } if ($r2==$r4) { $r4++; } $txt .= ShowBinary($r); $txt .= " "; $txt .= ShowBinary($r2); $txt .= " set "; $txt .= ShowBinary($r3); $txt .= " "; $txt .= ShowBinary($r4); $txt .= " set "; if (irand(2)==1) { $txt .= ShowBinary($r2); $txt .= " get "; $txt .= ShowBinary($r); $txt .= " =.\n"; } else { $txt .= ShowBinary($r2); $txt .= " get "; $txt .= ShowBinary($r); $txt .= " =.\n"; } } for (my $i=0; $i<12; $i++) { my $r = irand(16); my $r2 = irand(3); my $r3 = irand(16); $txt .= ShowBinary($r); $txt .= " "; $txt .= ShowBinary($r2); $txt .= " set "; $txt .= ShowBinary($r3); $txt .= " "; $txt .= ShowBinary($r2); $txt .= " get + "; $txt .= ShowBinary($r+$r3); $txt .= " =.\n"; } for (my $i=0; $i<12; $i++) { my $r = irand(16); my $r2 = irand(3); my $r3 = irand(16); $txt .= "?"; $txt .= " "; $txt .= ShowBinary($r2); $txt .= " set "; $txt .= ShowBinary($r3); $txt .= " "; $txt .= ShowBinary($r2); $txt .= " get + "; $txt .= ShowBinary($r+$r3); $txt .= " = "; $txt .= ShowBinary($r); $txt .= " !.\n"; } for (my $i=0; $i<8; $i++) { my $r = irand(16); $txt .= ShowBinary($r); $txt .= " 0 set 0 get 0 get * "; $txt .= ShowBinary($r*$r); $txt .= " =.\n"; } for (my $i=0; $i<8; $i++) { my $r = irand(16); $txt .= "? 0 set 0 get 0 get * "; $txt .= ShowBinary($r*$r); $txt .= " = "; $txt .= ShowBinary($r); $txt .= " !.\n"; } $txt .= "# have enough machinery for simple functions\n"; for (my $i=0; $i<8; $i++) { my $r = irand(10); my $r2 = irand(10); $txt .= "? 0 set 0 get 0 get * fx 0, set "; $txt .= ShowBinary($r); $txt .= " get 0, "; $txt .= ShowBinary($r2); $txt .= " ! + "; $txt .= ShowBinary($r2*$r2+$r); $txt .= " =.\n"; } $txt .= "# show side-effects : storage lasts across sentences\n"; for (my $i=0; $i<8; $i++) { my $r = irand(10); my $r2 = irand(10); $txt .= ShowBinary($r); $txt .= " "; $txt .= ShowBinary($r2); $txt .= " set.\n"; $txt .= ShowBinary($r2); $txt .= " get "; $txt .= ShowBinary($r); $txt .= " =.\n"; } # $txt .= "? 0 set 0 get 0 get * fx {SQUARE} set .\n"; return $txt; }; sub ShowRegisterCreationLesson { my $txt = ""; $txt .= "# set up some simple scope rules for registers\n"; for (my $i=0; $i<8; $i++) { my $r = irand(10); my $r2 = irand(10); my $r3 = irand(10); my $rr = irand(10); if ($r2==$r3) { $r2++; } if ($rr==$r) { $rr++; } $txt .= ShowBinary($r2); $txt .= " "; $txt .= ShowBinary($r); $txt .= " set "; $txt .= ShowBinary($r3); $txt .= " "; $txt .= ShowBinary($r); $txt .= " set "; $txt .= ShowBinary($r); $txt .= " get "; $txt .= ShowBinary($r3); $txt .= " =.\n"; $txt .= ShowBinary($r2); $txt .= " "; $txt .= ShowBinary($r); $txt .= " set "; $txt .= ShowBinary($r); $txt .= " nu "; $txt .= ShowBinary($r3); $txt .= " "; $txt .= ShowBinary($r); $txt .= " set "; $txt .= ShowBinary($r); $txt .= " un "; $txt .= ShowBinary($r); $txt .= " get "; $txt .= ShowBinary($r2); $txt .= " =.\n"; $txt .= ShowBinary($r2); $txt .= " "; $txt .= ShowBinary($r); $txt .= " set "; $txt .= ShowBinary($r); $txt .= " nu "; $txt .= ShowBinary($r3); $txt .= " "; $txt .= ShowBinary($r); $txt .= " set "; $txt .= ShowBinary($r); $txt .= " get "; $txt .= ShowBinary($rr); $txt .= " set "; $txt .= ShowBinary($r); $txt .= " un "; $txt .= ShowBinary($rr); $txt .= " get "; $txt .= ShowBinary($r3); $txt .= " =.\n"; } return $txt; }; sub ShowSimpleFunctionLesson { my $txt = ""; $txt .= "# show some simple function calls\n"; $txt .= "0 nu ? 0 set 0 get 0 get * 0 un {SQUARE} set.\n"; for (my $i=0; $i<8; $i++) { my $r = $i; #irand(10); $txt .= "{SQUARE} get "; $txt .= ShowBinary($r); $txt .= " ! "; $txt .= ShowBinary($r*$r); $txt .= " =.\n"; } for (my $i=0; $i<8; $i++) { my $r = $i; #irand(10); $txt .= "{SQUAREROOT} get "; $txt .= ShowBinary($r*$r); $txt .= " ! "; $txt .= ShowBinary($r); $txt .= " =.\n"; } return $txt; }; sub ShowIfLesson { my $txt = ""; $txt .= "# show mechanisms for branching\n"; for (my $i=0; $i<16; $i++) { my $r1 = irand(2); my $r2 = irand(10); my $r3 = irand(10); $txt .= ShowBinary($r2); $txt .= " "; $txt .= ShowBinary($r3); $txt .= " "; if ($r1) { $txt .= "true"; } else { $txt .= "false"; } $txt .= " if "; if ($r1) { $txt .= ShowBinary($r2); } else { $txt .= ShowBinary($r3); } $txt .= " =.\n"; } return $txt; }; # generate message # use consistent random choices # (really not acceptable to use random numbers, should use # look-up tables on a per lesson basis, to ensure stability # of sequences within lessons) srand(1); my $txt = ""; $txt .= "# Author: Paul Fitzpatrick, paulfitz\@ai.mit.edu # Copyright (c) 2003 Paul Fitzpatrick # # This file is part of CosmicOS. # # CosmicOS is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # CosmicOS is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with CosmicOS; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # "; $txt .= ShowUnaryLesson(); $txt .= ShowNotLogicLesson(); $txt .= ShowAndLogicLesson(); $txt .= ShowOrLogicLesson(); $txt .= ShowTrueFalseLesson(); $txt .= ShowAdditionLesson(); $txt .= ShowMultiplicationLesson(); $txt .= ShowDoubleLesson(); $txt .= ShowBinaryLesson(); $txt .= ShowEvaluationLesson(); $txt .= ShowSetGetLesson(); $txt .= ShowRegisterCreationLesson(); $txt .= ShowSimpleFunctionLesson(); $txt .= ShowIfLesson(); my $msg = Tokens2Msg(Text2Tokens($txt)); print "$txt\n\n"; $msg =~ s/\n//g; $msg =~ s/([a-z]{80})/$1\n*** /g; print "*** $msg\n"; # can do some verification of messages # my $t1 = $txt; # my @t2 = Text2Tokens($t1); # EvalCosmic($t1,@t2);