***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
<<TOC>>
***Preface***
[gold] 20aug2020 Here is extension of program written by Richard Suchenwirth [RS] in 2000, [Basic in TCL]. Called Basic_RS V2 in TCL as partial Basic language interpreter.
----
***Introduction***
Called Basic_RS V2 in TCL as partial Basic language interpreter. Learning experience, Added some easy eye console displays for my bad eyes. And trying some new features. Some math calculation forms seem to be working.
----
======
source basic_RS.tcl
basic { 10 n=2/7. : 20 print n} ;# returns 0.2857
basic { 10 n=[/ 2 7. ]: 20 print n} ;# returns 0.2857 basic { 10 n=[{2/ 2 7.}: ]20 print n}: 20 print n} :# returns 0.2857
# basic { 10 n=[ expr {2/ 7.} ] : 20 print n} ;#
# basic_RS.tcl not invoking expr smoothly
======
----
***Draft Wikipedia article on One Liners Programs , TCL Tool Control Language***
----
There is a gold mine of One Liners Programs and content in the Tool Control language TCL 8.6 core distribution, TCL manual pages, and TCLLIB library that can be adapted or recast into brief one liners programs. These one liners programs or procedures can be pasted into the TCL 8.6 console window for quick results, reference the TCL Wiki. Some one liners programs use the return statement, return $value, or return 1 to return the results to the program line. Although many TCL programmers just rely on the last computation being returned by the one liner procedure in the TCL console window. There is some room in the Wiki publications for programming style differences. But it is usually best to put usage, credits, or TCL documentation references for the one liners procedures on separate comment lines. The random procedures make use of the random function and do not return the same answer every time. Dependence on math operator notation, helper procedures, math check examples, and special library functions should be noted in the comment lines.
----
There are pros and cons to one liner programs in TCL. One may contrast the approach to one liners programs in problem solving versus the traditional procedural approach. There are better routines and methods in faster language constructs in the current TCL core distribution and TCLLIB. Working with recursion, primes, text search, and timing the procedures will quickly show the warts on the one liners programs. To gain speed and shorter computation times, one will generally have to access the TCL core distribution and TCLLIB. Since the TCL interpreter collapses the carriage returns, skips, blank lines, and dead space of traditional written procedures into a single line of machine code, is not every script a one liner program to the parser? As grandfather remarked, the gourmet omelet, beef mulligan stew, and farm buttermilk all go to the same place.
----
***Screenshots***
----
***figure 1. Basic_RS V1 as vaporware***
----
[basic_RS_in_TCL png]
----
***figure 2. Basic_RS V2 in TCL***
----
[Basic_RS V2 in TCL as partial Basic language interpreter screenshot]
----
***References:***
----
* google search engine < Quickbasic>
* [BASIC]
* en.wikipedia.org wiki BASIC
* en.wikipedia.org wiki QBasic
* [Call Procedure Like Fortran Example]
* [Basic in TCL]
* [https://en.wikipedia.org/wiki/One-liner_program] Draft Wikipedia article on One Liners Programs , TCL Tool Control Language
* [https://en.wikipedia.org/wiki/QBasic] QBasic wikipedia
----
----
----
----
****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**
<<discussion>>
Please include your wiki MONIKER and date in your comment with the same courtesy that I will give you. Thanks, [gold] 12Aug2020
----
<<categories>> Numerical Analysis | Toys | Calculator | Mathematics| Example| Toys and Games | Games | Application | GUI
----
<<categories>> Development | Concept| Algorithm | Language