fbcadcam-macro

User projects written in or related to FreeBASIC.
owen
Posts: 506
Joined: Apr 19, 2006 10:55
Location: Kissimmee, FL
Contact:

fbcadcam-macro

Postby owen » Sep 19, 2016 16:26

fbcadcam macro by Owen P. Reese
email opreese@gmail.com
websites fbcadcam.com / fbcadcam.net / fbcadcam.org
cell# (407) 655-8537

'open source - no restrictions

'description: script engine for fbcadcam macros
'source code:
'http://fbcadcam.org/fbcadcam-macro/
'fbcadcam-macro-0.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
Last edited by owen on Sep 22, 2016 17:12, edited 3 times in total.
owen
Posts: 506
Joined: Apr 19, 2006 10:55
Location: Kissimmee, FL
Contact:

Re: fbcadcam-macro

Postby owen » Sep 22, 2016 12:40

fbcadcam-macro-0.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()
owen
Posts: 506
Joined: Apr 19, 2006 10:55
Location: Kissimmee, FL
Contact:

Re: fbcadcam-macro

Postby owen » Sep 22, 2016 14:50

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()
owen
Posts: 506
Joined: Apr 19, 2006 10:55
Location: Kissimmee, FL
Contact:

Re: fbcadcam-macro

Postby owen » Oct 16, 2016 1:51

'fbcadcam.org/fbcadcam-macro/fbcadcam-macro-0.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
owen
Posts: 506
Joined: Apr 19, 2006 10:55
Location: Kissimmee, FL
Contact:

Re: fbcadcam-macro

Postby owen » Oct 17, 2016 14:09

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/fbcadcam-macro/20161016_162035.jpg
srvaldez
Posts: 1834
Joined: Sep 25, 2005 21:54

Re: fbcadcam-macro

Postby srvaldez » Oct 17, 2016 14:36

vdecampo wrote KwikBASIC viewtopic.php?f=8&t=14321&hilit=KwikBASIC
which is a byte-code basic interpreter resembling FB
owen
Posts: 506
Joined: Apr 19, 2006 10:55
Location: Kissimmee, FL
Contact:

Re: fbcadcam-macro

Postby owen » Oct 19, 2016 6:32

thanks srvaldez
speed test comparison on my old laptop of kwikbasic testcode.bas
fbc .002 seconds
fbcadcam-macro 8 seconds
kwikbasic 27 seconds
Mysoft bytecode example ? seconds
owen
Posts: 506
Joined: Apr 19, 2006 10:55
Location: Kissimmee, FL
Contact:

Re: fbcadcam-macro

Postby owen » Oct 04, 2017 5:52

my latest work on this project is
http://fbcadcam.org/fbcadcam-macro/fbca ... ings-6.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
integer
Posts: 375
Joined: Feb 01, 2007 16:54
Location: usa

Re: fbcadcam-macro

Postby integer » Oct 04, 2017 7:22

owen wrote:my latest work on this project is
http://fbcadcam.org/fbcadcam-macro/fbca ... ings-6.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: (Xmax-Xmin)*(Ymax-Ymin)=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: (Xmax-Xmin)*(Ymax-Ymin)=Area01
rotate again; find Max/Min x coord, and the max/min y coord: (Xmax-Xmin)*(Ymax-Ymin)=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.
owen
Posts: 506
Joined: Apr 19, 2006 10:55
Location: Kissimmee, FL
Contact:

Re: fbcadcam-macro

Postby owen » Oct 04, 2017 10:51

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.

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(") _

owen
Posts: 506
Joined: Apr 19, 2006 10:55
Location: Kissimmee, FL
Contact:

Re: fbcadcam-macro

Postby owen » Oct 04, 2017 13:09

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:

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
owen
Posts: 506
Joined: Apr 19, 2006 10:55
Location: Kissimmee, FL
Contact:

Re: fbcadcam-macro

Postby owen » Oct 04, 2017 13:53

....
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.
D.J.Peters
Posts: 7659
Joined: May 28, 2005 3:28

Re: fbcadcam-macro

Postby D.J.Peters » Oct 04, 2017 19:01

@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
owen
Posts: 506
Joined: Apr 19, 2006 10:55
Location: Kissimmee, FL
Contact:

Re: fbcadcam-macro

Postby owen » Oct 04, 2017 22:53

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
owen
Posts: 506
Joined: Apr 19, 2006 10:55
Location: Kissimmee, FL
Contact:

Re: fbcadcam-macro

Postby owen » Oct 04, 2017 23:07

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.

Return to “Projects”

Who is online

Users browsing this forum: No registered users and 5 guests