fbcadcam macro by Owen P. Reese
email opreese@gmail.com
websites fbcadcam.com / fbcadcam.net / fbcadcam.org
cell# (407) 6558537
'open source  no restrictions
'description: script engine for fbcadcam macros
'source code:
'http://fbcadcam.org/fbcadcammacro/
'fbcadcammacro0.1.bas
'test.bas is your source code to run as a macro
currently supports commands:
Dim  is limited to integers and doubles ie. dim x as integer, dim as double x
For Next Step
Do Loop
Do While Loop
Do Until Loop
If Then Else
Print  print is restricted to printing integers and doubles ie. print 1, print 1.1, print x
fbcadcammacro
fbcadcammacro
Last edited by owen on Sep 22, 2016 17:12, edited 3 times in total.
Re: fbcadcammacro
fbcadcammacro0.1.1.bas
new to this version: Sub keyword_select(select_temps() As String,select_temp_c As Integer)
select case
case x
case is > x
case x to y
case else
also i changed test.bas to macro.bas in Sub import_macro_bas_file()
new to this version: Sub keyword_select(select_temps() As String,select_temp_c As Integer)
select case
case x
case is > x
case x to y
case else
also i changed test.bas to macro.bas in Sub import_macro_bas_file()
Re: fbcadcammacro
This program reads a text file (macro.bas), checking each line for recognized freebasic syntax. In version 0.1.1 it recognizes keywords:
Dim As Integer, Double
For Next Step Exit For
Do Loop While Until Exit Do
If Then Else Endif
Select Case Case Is To End Select
Print
Note: Keywords Continue For, Elseif and others have not been implemented yet.
Recognized Arithmetic operators ( +  * / \ ^ mod shl shr )
Recognized Bitwise operators ( and eqv imp or not xor )
Recognize Math operators ( abs acos asin atan2 atn cos exp fix frac int log rnd sgn sin sqr tan )
How it works:
Lets say macro.bas has a few lines of code such as:
Dim As Integer X, Y, Z(3)
X=1
Y=2
Z(1) = X
Z(2) = Y
Z(3) = X + Y
Print Z(1)
Print Z(2)
Print Z(3)
The first line has a recognized keyword DIM
Function keyword_commands_block(temp As String)
If Left(temp,4)="DIM " Then keyword_dim(temp)
Sub keyword_dim(temp As String)
This sub routine parses the rest of the line ie. As Integer X, Y, Z
It recognizes the keyword AS Integer and that it has commas signifying multiple variables declarations.
Here there are 3 variables each with their own name, type and values
The string array called macro_variables_info() stores variable Names , Types, indexes, and array_description
macro_variables_info(1,1) = “X”
macro_variables_info(1,2) = “INTEGER”
macro_variables_info(1,3) = “1” ‘This “1” (one) points to the first index value of macro_variables_integer_value()
macro_variables_info(1,4) = is not used because this variable is NOT an array
The values of X and Y are added to the array called macro_variables_integer_value()
The values of Z() are added to the array called macro_variables_integer_array_1d_1()
Currently this program affords unlimited usage of NON array integer and double variables.
Currently this program affords LIMITED usage of Arrays with a maximum of:
Three 1 D integer arrays
Three 2 D integer arrays
Three 3 D integer arrays
Three 1 D double arrays
Three 2 D double arrays
Three 3 D double arrays.
If you want to use more then THREE 1D, 2D, 3D:
For example if you want to use FIVE 1D arrays then:
Dim Shared As Integer macro_variables_integer_array_1d_1()
Dim Shared As Integer macro_variables_integer_array_1d_2()
Dim Shared As Integer macro_variables_integer_array_1d_3()
Dim Shared As Integer macro_variables_integer_array_1d_4()
Dim Shared As Integer macro_variables_integer_array_1d_5()
The arithmetic: Z(3) = X + Y is handled in Function evaluate_expression() and returns a value for Z(3) which was called from Sub keyword_let()
Dim As Integer, Double
For Next Step Exit For
Do Loop While Until Exit Do
If Then Else Endif
Select Case Case Is To End Select
Note: Keywords Continue For, Elseif and others have not been implemented yet.
Recognized Arithmetic operators ( +  * / \ ^ mod shl shr )
Recognized Bitwise operators ( and eqv imp or not xor )
Recognize Math operators ( abs acos asin atan2 atn cos exp fix frac int log rnd sgn sin sqr tan )
How it works:
Lets say macro.bas has a few lines of code such as:
Dim As Integer X, Y, Z(3)
X=1
Y=2
Z(1) = X
Z(2) = Y
Z(3) = X + Y
Print Z(1)
Print Z(2)
Print Z(3)
The first line has a recognized keyword DIM
Function keyword_commands_block(temp As String)
If Left(temp,4)="DIM " Then keyword_dim(temp)
Sub keyword_dim(temp As String)
This sub routine parses the rest of the line ie. As Integer X, Y, Z
It recognizes the keyword AS Integer and that it has commas signifying multiple variables declarations.
Here there are 3 variables each with their own name, type and values
The string array called macro_variables_info() stores variable Names , Types, indexes, and array_description
macro_variables_info(1,1) = “X”
macro_variables_info(1,2) = “INTEGER”
macro_variables_info(1,3) = “1” ‘This “1” (one) points to the first index value of macro_variables_integer_value()
macro_variables_info(1,4) = is not used because this variable is NOT an array
The values of X and Y are added to the array called macro_variables_integer_value()
The values of Z() are added to the array called macro_variables_integer_array_1d_1()
Currently this program affords unlimited usage of NON array integer and double variables.
Currently this program affords LIMITED usage of Arrays with a maximum of:
Three 1 D integer arrays
Three 2 D integer arrays
Three 3 D integer arrays
Three 1 D double arrays
Three 2 D double arrays
Three 3 D double arrays.
If you want to use more then THREE 1D, 2D, 3D:
For example if you want to use FIVE 1D arrays then:
Dim Shared As Integer macro_variables_integer_array_1d_1()
Dim Shared As Integer macro_variables_integer_array_1d_2()
Dim Shared As Integer macro_variables_integer_array_1d_3()
Dim Shared As Integer macro_variables_integer_array_1d_4()
Dim Shared As Integer macro_variables_integer_array_1d_5()
The arithmetic: Z(3) = X + Y is handled in Function evaluate_expression() and returns a value for Z(3) which was called from Sub keyword_let()
Re: fbcadcammacro
'fbcadcam.org/fbcadcammacro/fbcadcammacro0.2.bas
added subs and functions
sub test(n as integer)
dim as integer a,b,c
a=1
b=2
c=3
print a,b,c
end sub
_______________________________
variable names are changed to:
_______________________________
sub test(test_n as integer)
dim as integer test_a,test_b,test_c
test_a=1
test_b=2
test_c=3
print test_a,test_b,test_c
end sub
added subs and functions
sub test(n as integer)
dim as integer a,b,c
a=1
b=2
c=3
print a,b,c
end sub
_______________________________
variable names are changed to:
_______________________________
sub test(test_n as integer)
dim as integer test_a,test_b,test_c
test_a=1
test_b=2
test_c=3
print test_a,test_b,test_c
end sub
Re: fbcadcammacro
update version 0.2.1
added code to clear variable values for subs and functions upon each call.
yesterday I was informed by +Mysoft that using bytecode for this project is the better solution and I may rewrite the whole thing once I get a better handle on how bytecode works. until then I'll just keep on working on this as it is.
just so you all know, yes I agree that the massive / excessive string manipulation method I am using in this interpreter is e x t r e e m l y slow which is perfectly fine by me as long as it works. my intention is to use this in my cad project so my users can write and run macros to do simple things like rotate selected objects in the drawing. in other words, the macro will run and complete the cad users objective and then stop running. if it takes 2 seconds to interpret and run the macro instead of a half a second with an optimize bytecode version then I think I can live with that. but I am not against learning how to do this bytecode stuff. it sounds really cool.
if anyone manages write a fb syntax compatible interpreter using bytecode would someone please tell me. thanx in advance.
ps. here's a picture of me mulling over yesterdays conversation on the chat.
'http://fbcadcam.org/fbcadcammacro/20161016_162035.jpg
added code to clear variable values for subs and functions upon each call.
yesterday I was informed by +Mysoft that using bytecode for this project is the better solution and I may rewrite the whole thing once I get a better handle on how bytecode works. until then I'll just keep on working on this as it is.
just so you all know, yes I agree that the massive / excessive string manipulation method I am using in this interpreter is e x t r e e m l y slow which is perfectly fine by me as long as it works. my intention is to use this in my cad project so my users can write and run macros to do simple things like rotate selected objects in the drawing. in other words, the macro will run and complete the cad users objective and then stop running. if it takes 2 seconds to interpret and run the macro instead of a half a second with an optimize bytecode version then I think I can live with that. but I am not against learning how to do this bytecode stuff. it sounds really cool.
if anyone manages write a fb syntax compatible interpreter using bytecode would someone please tell me. thanx in advance.
ps. here's a picture of me mulling over yesterdays conversation on the chat.
'http://fbcadcam.org/fbcadcammacro/20161016_162035.jpg
Re: fbcadcammacro
vdecampo wrote KwikBASIC viewtopic.php?f=8&t=14321&hilit=KwikBASIC
which is a bytecode basic interpreter resembling FB
which is a bytecode basic interpreter resembling FB
Re: fbcadcammacro
thanks srvaldez
speed test comparison on my old laptop of kwikbasic testcode.bas
fbc .002 seconds
fbcadcammacro 8 seconds
kwikbasic 27 seconds
Mysoft bytecode example ? seconds
speed test comparison on my old laptop of kwikbasic testcode.bas
fbc .002 seconds
fbcadcammacro 8 seconds
kwikbasic 27 seconds
Mysoft bytecode example ? seconds
Re: fbcadcammacro
my latest work on this project is
http://fbcadcam.org/fbcadcammacro/fbca ... ings6.bas
need some help beta testing it.
it interprets source code saved in file "test.bas"
this is a work in progress. there is a lot pending.
example it should interpret:
dim as integer i
for i=1 to 26
print chr(i+64)
next
http://fbcadcam.org/fbcadcammacro/fbca ... ings6.bas
need some help beta testing it.
it interprets source code saved in file "test.bas"
this is a work in progress. there is a lot pending.
example it should interpret:
dim as integer i
for i=1 to 26
print chr(i+64)
next
Re: fbcadcammacro
owen wrote:my latest work on this project is
http://fbcadcam.org/fbcadcammacro/fbca ... ings6.bas
need some help beta testing it.
it interprets source code saved in file "test.bas"
this is a work in progress. there is a lot pending.
example it should interpret:
dim as integer i
for i=1 to 26
print chr(i+64)
next
@owen: WOW!
30k+ lines of code
I have run a few very simple short routines; impressive. Very good.
A Rotation/Translation of a 2D coordinate set is my goal.
Actually this: I have a set of 2d coords.
I want to print these (with point numbers) on letter size (or legal size) paper, using the entire sheet.
At the moment, I find the Max/Min x coord, and the max/min y coord: (XmaxXmin)*(YmaxYmin)=Area00
Rotate the coordinates 1 degree (clockwise or CCW does not matter), then determine the
the Max/Min x coord, and the max/min y coord: (XmaxXmin)*(YmaxYmin)=Area01
rotate again; find Max/Min x coord, and the max/min y coord: (XmaxXmin)*(YmaxYmin)=Area02
This is done for 45 degrees, in increments of 1 degree. The maximum area is calculated.
From the maximum area, then scale the coordinates to fit the page size needed.
Anyway, you have a fine program.
I will do some testing to see where it burps.
Thanks.
Re: fbcadcammacro
I like that phrase. "BURP TESTED"
New in this version are the string functions and dtc's (data type conversion) functions ie. CHR, INSTR,... CINT, CDBL,...
The bitwise ops were moved around and reworked.
AndAlso, OrElse were included.
String concatenation operaters + and & are still pending.
New in this version are the string functions and dtc's (data type conversion) functions ie. CHR, INSTR,... CINT, CDBL,...
The bitwise ops were moved around and reworked.
AndAlso, OrElse were included.
String concatenation operaters + and & are still pending.
Code: Select all
arithmetic_op_cmd_c=9
arithmetic_op_cmd(1)="+"
arithmetic_op_cmd(2)=""
arithmetic_op_cmd(3)="*"
arithmetic_op_cmd(4)="/"
arithmetic_op_cmd(5)="\"
arithmetic_op_cmd(6)="^"
arithmetic_op_cmd(7)=" mod "
arithmetic_op_cmd(8)=" shl "
arithmetic_op_cmd(9)=" shr "
bitwise_op_cmd_c=8
bitwise_op_cmd(1)="not "
bitwise_op_cmd(2)=" and "
bitwise_op_cmd(3)=" or "
bitwise_op_cmd(4)=" eqv "
bitwise_op_cmd(5)=" imp "
bitwise_op_cmd(6)=" xor "
bitwise_op_cmd(7)=" andalso "
bitwise_op_cmd(8)=" orelse "
math_dtc_op_cmd_c=31
math_dtc_op_cmd(1)="cbyte("
math_dtc_op_cmd(2)="cubyte("
math_dtc_op_cmd(3)="cshort("
math_dtc_op_cmd(4)="cushort("
math_dtc_op_cmd(5)="clng("
math_dtc_op_cmd(6)="culng("
math_dtc_op_cmd(7)="cint("
math_dtc_op_cmd(8)="cuint("
math_dtc_op_cmd(9)="clngint("
math_dtc_op_cmd(10)="culngint("
math_dtc_op_cmd(11)="csign("
math_dtc_op_cmd(12)="cunsg("
math_dtc_op_cmd(13)="csng("
math_dtc_op_cmd(14)="cdbl("
math_dtc_op_cmd(15)="cbool("
math_dtc_op_cmd(16)="abs("
math_dtc_op_cmd(17)="acos("
math_dtc_op_cmd(18)="asin("
math_dtc_op_cmd(19)="atan2("
math_dtc_op_cmd(20)="atn("
math_dtc_op_cmd(21)="cos("
math_dtc_op_cmd(22)="exp("
math_dtc_op_cmd(23)="fix("
math_dtc_op_cmd(24)="frac("
math_dtc_op_cmd(25)="int("
math_dtc_op_cmd(26)="log("
math_dtc_op_cmd(27)="rnd("
math_dtc_op_cmd(28)="sgn("
math_dtc_op_cmd(29)="sin("
math_dtc_op_cmd(30)="sqr("
math_dtc_op_cmd(31)="tan("
If _
InStr(temp,"chr(") Or _
InStr(temp,"str(") Or _
InStr(temp,"lcase(") Or _
InStr(temp,"ucase(") Or _
InStr(temp,"string(") Or _
InStr(temp,"space(") Or _
InStr(temp,"right(") Or _
InStr(temp,"left(") Or _
InStr(temp,"mid(") Or _
InStr(temp,"trim(") Or _
InStr(temp,"ltrim(") Or _
InStr(temp,"rtrim(") Or _
InStr(temp,"instr(") Or _
InStr(temp,"instrrev(") Or _
InStr(temp,"len(") Or _
InStr(temp,"asc(") Or _
InStr(temp,"val(") _
Re: fbcadcammacro
When I first started working on this project, my goal was to let fbcad user draw programatically should they so desire, ie. be able to run user written macros (scripts) in fbcad. Naturally I figured the macro language and synax should be similar to FB.
Starting from the simplest idea of parsing, interpreting/running a single line of code to draw a line:
to interpreting multi lines of code using variables and nested For Next / Do While loops
Starting from the simplest idea of parsing, interpreting/running a single line of code to draw a line:
Code: Select all
Line(0,0)(100,100)
to interpreting multi lines of code using variables and nested For Next / Do While loops
Code: Select all
Dim As Integer x,y
For x=1 to 10
For y=1 to 20
Line(0,0)(x,y)
Next
Next
Re: fbcadcammacro
....
to 1d, 2d and 3d Arays
to using math functoins such as SIN, COS, TAN, etc...
to Sub Routines and Functions
and now Strings.
My goal regarding strings is to let the user write scripts that can manipulate text for the purpose of inserting text into the drawing.
And shortly there after strings will aford the user to define / manipulate file names in cunjunction with the Open command (not implimented yet).
This is a work in progress. Adding more functionality tothis interpreter over time may prove to be a cool semi interpreter for FB but that is probably not going to happen in my life time because FB is huge.
to 1d, 2d and 3d Arays
to using math functoins such as SIN, COS, TAN, etc...
to Sub Routines and Functions
and now Strings.
My goal regarding strings is to let the user write scripts that can manipulate text for the purpose of inserting text into the drawing.
And shortly there after strings will aford the user to define / manipulate file names in cunjunction with the Open command (not implimented yet).
This is a work in progress. Adding more functionality tothis interpreter over time may prove to be a cool semi interpreter for FB but that is probably not going to happen in my life time because FB is huge.

 Posts: 7852
 Joined: May 28, 2005 3:28
Re: fbcadcammacro
@owen good job
30K of lines for an limited script/BASIC interpreter that is huge but if it does the right job it's OK
I will do a short look at your work may be I find out why it's so large.
Joshy
30K of lines for an limited script/BASIC interpreter that is huge but if it does the right job it's OK
I will do a short look at your work may be I find out why it's so large.
Joshy
Re: fbcadcammacro
30k LOC can probably be reduced to 5k or less if I can figure out a better technique for ARRAYS and multi variable types.
One thought is to hold all values for numeric data types whether they be arrays or not in a large 1D array and manage a few smaller arrays containing:
variable name
Variable size (Lbound to Ubond if array or 1 if just a single variable)
variable type (integer, short, byte, long, double etc...)
variable starting position within the large 1D array.
etc...
But this idea requires a lot of shuffling in the large 1d array in the event of REDIM
One thought is to hold all values for numeric data types whether they be arrays or not in a large 1D array and manage a few smaller arrays containing:
variable name
Variable size (Lbound to Ubond if array or 1 if just a single variable)
variable type (integer, short, byte, long, double etc...)
variable starting position within the large 1D array.
etc...
But this idea requires a lot of shuffling in the large 1d array in the event of REDIM
Re: fbcadcammacro
Another idea about arrays is to contain all
1D arrays in a LARGE 2D array
2D arrays in a LARGE 3D array
3D arrays in a LARGE 4D array
LARGE 2D array because it needs to hold the largest 1D array and a whole bunch of wasted memory for all the other much smaller 1D arrays.
1D arrays in a LARGE 2D array
2D arrays in a LARGE 3D array
3D arrays in a LARGE 4D array
LARGE 2D array because it needs to hold the largest 1D array and a whole bunch of wasted memory for all the other much smaller 1D arrays.
Who is online
Users browsing this forum: Xusinboy Bekchanov and 1 guest