Basic_RS V2 in TCL as partial Basic language interpreter

Basic_RS V2 in TCL as partial Basic language interpreter

This page is under development. Comments are welcome, but please load any comments in the comments section at the bottom of the page. Please include your wiki MONIKER and date in your comment with the same courtesy that I will give you. Aside from your courtesy, your wiki MONIKER and date as a signature and minimal good faith of any internet post are the rules of this TCL-WIKI. Its very hard to reply reasonably without some background of the correspondent on his WIKI bio page. Thanks, gold 20Aug2020


Title: Demo Basic_RS V2

Preface

gold 20aug2020 Here is extension of program written by Richard Suchenwirth in 2001, Basic in TCL. Called Basic_RS V2 in TCL as partial Basic language interpreter.


Introduction

Here is extension of program written by Richard Suchenwirth RS in 2001. Called Basic_RS V2 in TCL as partial Basic language interpreter. Learning experience, Added some console displays, and trying some new features.


Basic_RS V2 in TCL as partial Basic language interpreter screenshot


References:


  • google search engine < Quickbasic>




Appendix TCL programs and scripts

* Pretty Print Version


        # Title: Basic_RS in TCL V2
        # partial basic language interpreter Basic_RS
        # demo basic from RS, (wiki 2000-08-21)
        # Original program  by Richard Suchenwirth on TCL WIKI
        # Reorganized code from, https://wiki.tcl-lang.org/915
        # for print and self_help 
        # to tcl console.
        # written on Windows 10 on TCL
        # working under TCL version 8.6
        # on TCL WIKI , 20Sep2020
        # added statements for TCLLIB library
        package require math::numtheory
        package require math::constants
        package require math::trig
        package require math
        namespace path {::tcl::mathop ::tcl::mathfunc math::numtheory math::trig math::constants }
        # Tried to keep clean RS  code in upper section
        # added extension to console below. -gold
        proc lpush {_list what} {
            upvar $_list L
            if ![info exists L] {set L {}}
            set L [concat [list $what] $L]
        }
        proc lpop {_list} {
            upvar $_list L
            if ![info exists L] {return ""}
            set t [lindex $L 0]
            set L [lrange $L 1 end]
            return $t
        }
        proc intgen {{seed 0}} {
            set self [lindex [info level 0] 0]
            proc $self "{seed [incr seed]}" [info body $self]
            set seed
        } ;# RS       
        proc basic {script} {
            # old BASIC had 26 numeric variables..
            foreach i {A B C D E F G H I J K L M N O P Q R S T U V W X Y Z} {
                set $i 0
            }
            set states ""
            set Stack [list Error:Stack]
            foreach line [split [string toupper $script] \n] {
                if [regexp { *([0-9]+) +(.+)} $line -> label rest] {
                    set outline ""
                    foreach stmt [split $rest :] {
                        set step 1
                        if [regexp { ?('|REM)} $stmt] {
                            # start extension for console
                            # wish to print all comments
                            puts $stmt
                            break ;# no statements to be expected after comment                       
                         } elseif [regexp { ?('|PRINT_TXT)} $stmt] { 
                            # wish to print specific text
                            puts $stmt
                            break ;# no statements to be expected
                         } elseif [regexp { ?('|EXPR)} $stmt]  { 
                            # wish eval content to expr
                             # $w move [$w find withtag "$tilex"] [expr {$x-$p(X)}] [expr {$y-$p(Y)}]
                             #set numberx [$w  gettags current]
                             #regexp {obj_(\d+)} $numberx -> tilex
                             puts " trick1   \[expr $stmt \] \[expr {[lindex [split $stmt ] 3 ]}\]" ;
                             puts " trick2  [expr   [expr {[lindex [split $stmt ] 3 ]} ]]" ;
                             puts " trick3    [expr {[lindex [split $stmt ] 3 ]} ] " ;
                             set tilex 7777;
                             regexp {EXPR(\a+)} $stmt -> tilex;
                            puts  $tilex;
                            break ;# no statements to be expected
                            # end extension for console.
                        } elseif [regexp {PRINT +(.+)} $stmt -> what] {
                            regsub -all ", *" $what "\t" what
                            regsub -all "; *" $what " " what
                            regsub -all {([A-Z])} $what {$\1} what
                            append outline "puts \"$what\"; "
                        } elseif [regexp {FOR +([A-Z]) *= *(.+) TO (.+)( STEP (.+))?} \
                                $stmt -> looper from to - step] {
                                    regsub -all {([A-Z])} $from {$\1} from
                                    regsub -all {([A-Z])} $to {$\1} to
                                    set here -[intgen]
                                    set step 1 ;# to start with
                                    set next($looper) "incr $looper $step; if {\$$looper<=$to} {goto $here}"
                                    append outline "set $looper $from\}\n$here \{"
                                } elseif [regexp {NEXT (.+)} $stmt -> id] {
                                    append outline $next($id)
                                    unset next($id)
                                } elseif [regexp {IF +([^ ]+) +(THEN|GOTO) +([0-9]+)}\
                                $stmt -> cond - goto] {
                                    regsub -all {([A-Z])} $cond {$\1} cond
                                    append outline "if {$cond} {goto $goto}; "
                                } elseif [regexp {ON (.+) GOTO +(.+)} $stmt -> cond labels] {
                                    regsub -all {([A-Z])} $cond {$\1} cond
                                    append outline "goto \[lindex \{- [split $labels ,]\} \[expr $cond\]\]; "
                                } elseif [regexp {GO *TO ([0-9]+)} $stmt -> id] {
                                    append outline "goto $id; "
                                } elseif [regexp {GO *SUB ([0-9]+)} $stmt -> id] {
                                    set here -[intgen]
                                    append outline "lpush Stack $here; goto $id\}\n$here \{ "
                                } elseif [regexp {RETURN} $stmt] {
                                    append outline "goto \[lpop Stack\]; "
                                } elseif [regexp { *END} $stmt] {
                                    append outline "break; "
                                } elseif [regexp {([A-Z])=(.+)} $stmt -> lhs rhs] {
                                    regsub -all {([A-Z])} $rhs {$\1} rhs
                                    append outline "set $lhs \[expr $rhs\]; "
                                }
                    }
                    append states "$label {$outline}\n"
                }
            }
            states $states
        } ;#RS        
        proc states body {
            proc goto {id} {uplevel set goto $id; return -code continue}
            uplevel set goto [lindex $body 0]
            set tmp [lindex $body 0]
            foreach {cmd label} [lrange $body 1 end] {
                if {$label==""} {set label default}
                lappend tmp "$cmd; goto [list $label]" $label
            }
            lappend tmp break ;# to match last "default" label
            uplevel while 1 "{switch -- \$goto [list $tmp]}"
            rename goto ""
        } ;#RS        
        basic {
            100 J=1:K=1
            110 GOSUB 300
            120 PRINT I,J;K
            130 IF I<5 GOTO 110: REM DONE
            135 for a=100 to J+103
            137    print a
            140 next a
            150 END
            300 REM---------- slight increment
            310 I=I+J: K=K*2
            320 RETURN
        }
        #end of RS deck       
        # add cosmetics below to bottom of file or source Basic_RS.tcl
        # added statements above for TCLLIB library
        console show
        console eval {.console config -bg palegreen}
        console eval {.console config -font {fixed 20 bold}}
        console eval {wm geometry . 40x20}
        console eval {wm title . "Basic_RS in TCL , screen grab and paste from console  to texteditor"}
        console eval {. configure -background orange -highlightcolor brown -relief raised -border 30}
        console eval { proc self_helpx {} {
                set msg "Basic_RS in TCL, large black type on green
                from TCL,
                self help listing
                Conventional text editor formulas grabbed
                from internet screens can be pasted
                into green console
                colon is statement end
                # demo basic from RS, (wiki 2000-08-21) "
                tk_messageBox -title "self_helpxx" -message $msg } }
        console eval {.menubar.help add command -label Self_help -command self_helpx } 
        # proc basic here is a second program or set of commands
            basic {
            50 REM user on basic_RS, TCL 8.6
            70 REM math ops from TCL is loaded
            100 a=1.
            137 print a
            140 b=  [* 5 6 ]             
            150 print b             
            160 c= [/ 5 7. ] 
            170 print c
            175 REM PRINTING D 
            180 d=5*5 
            200 print d
            250 print [* 5 7 ]
            300 REM wish to print all REM strings
            315 PRINT_TXT user on TCL 8.6
            318 PRINT_TXT  [* 5 6 ]
            340 REM wish to use expr math
            350 EXPR  { 2*7 }
            400 RETURN
            }
            puts " returns to TCL 8.6  for check 5*6 = [* 5 6 ]"  
        

Hidden Comments Section

Please include your wiki MONIKER and date in your comment with the same courtesy that I will give you. Thanks, gold 12Aug2020