changeset 1:7e03f04b23ec

first simple translateor to C
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Wed, 14 Oct 2020 18:50:31 +0900
parents 76f88d2d6cd6
children 8fa50012cad9
files tl1trans/tl1trans.pl
diffstat 1 files changed, 28 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/tl1trans/tl1trans.pl	Wed Oct 14 18:22:25 2020 +0900
+++ b/tl1trans/tl1trans.pl	Wed Oct 14 18:50:31 2020 +0900
@@ -83,17 +83,15 @@
             }
             print <<"EOFEOF";
 #include <stdio.h>
+#include <string.h>
+#include <unistd.h>
 
-void write() {
-}
-    
 $gvar;
 $procs;
 $funcs;
 
 int main(int ac, char *av[]) {
 EOFEOF
-            &getline;
             &statement;
             print <<"EOFEOF";
    return 0;
@@ -111,9 +109,31 @@
         while($_ ne "") {
             if (s/^WRITE\((\d+)://)  {
                 my $d = $1;
-                while(s/.//) {
-                    if ($& eq ")")  {
-                       &output(" write(); ");
+                while($_ ne "") {
+                    if (s/^\"//) {
+                       my $str = "";
+                       while(s/.//) {
+                           last if ($& eq '"') ;
+                           if ($& eq '\\') {
+                               if (s/.//) {
+                                   if ($& eq "\"") { $str .= "\\\"" ;
+                                   } else { $str .= "\\$&" ; }
+                               }
+                           } else {
+                               $str .= $&;
+                           }
+                       }
+                       if ($str ne "") {
+                           $str = "\"$str\"";
+                           print "write($d,$str,strlen($str));"
+                       }
+                    } elsif (s/^CRLF//) {
+                        print "write($d,\"\\n\",1);";
+                    } elsif (s/^\w+//) {
+                        print "dprintf($d,\"%d\",$&);";
+                    } elsif (s/.//) {
+                    } elsif ($& eq ")")  {
+                        last;
                     }
                 }
             } elsif (s/^[[({]//) {
@@ -160,10 +180,9 @@
 $lvar;
 EOFEOF
         } elsif (/^BEGIN/) {
-            &getline;
             &statement;
             print <<"EOFEOF";
-// end $proc
+} // end $proc
 EOFEOF
         } elsif (/^(\w+)/) {
             $proc = $1;