LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-14-2014, 10:22 PM   #301
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,604

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241

It is a way to try.
 
Old 10-17-2014, 06:08 PM   #302
schmitta
Member
 
Registered: May 2011
Location: Blacksburg VA
Distribution: UBUNTU, LXLE
Posts: 308

Original Poster
Rep: Reputation: Disabled
You may want to down load a copy of this book - Real Time Forth ftp://ftp.taygeta.com/pub/forth/Literature/rtfv5.pdf I have printed it out at FEDEX for $ 41 and change. and am reading through it. it is great.
 
Old 10-17-2014, 06:59 PM   #303
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,604

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
Thanks. I have some reading to do...
 
Old 11-16-2014, 03:25 PM   #304
schmitta
Member
 
Registered: May 2011
Location: Blacksburg VA
Distribution: UBUNTU, LXLE
Posts: 308

Original Poster
Rep: Reputation: Disabled
SVFIG Forth Day https://www.youtube.com/watch?featur...&v=VbuiqHD6dXI
 
Old 11-19-2014, 11:20 PM   #305
schmitta
Member
 
Registered: May 2011
Location: Blacksburg VA
Distribution: UBUNTU, LXLE
Posts: 308

Original Poster
Rep: Reputation: Disabled
found a tiny basic written in forth which I will try to reproduce here:

http://www.nicholson.com/rhn/files/T...C_in_Forth.txt
Code:
\ BASIC compiler				      06Feb84map
ONLY FORTH ALSO DEFINITIONS
: .R  RP0 @ RP@ ?DO  I @ 2- @ >NAME .ID	 2 +LOOP ;
VOCABULARY ARITHMETIC	ARITHMETIC ALSO DEFINITIONS
VOCABULARY LOGIC   VOCABULARY INPUTS   VOCABULARY OUTPUTS
: [   ASCII ] WORD DROP ;  IMMEDIATE
: GET	BL WORD NUMBER DROP  ;
CREATE #S  130 ALLOT
FORTH DEFINITIONS
1 2 +THRU   ( precedence and variables )
: BASIC	 [ ARITHMETIC ]	 0 #S 2+ #S 2!	START ALSO ; IMMEDIATE
ARITHMETIC DEFINITIONS
3 7 +THRU   ( BASIC )
: (   10 #( +! ;  IMMEDIATE
: ;   [ n]   . ;   1 PRECEDENCE ;
FORTH DEFINITIONS
\ Precedence					      06Feb84map
VARIABLE ADDRESS   VARIABLE #(
: )   -10 #( +!	 #( @ 0< ABORT" Unmatched )" ;	IMMEDIATE
: DEFER	 ( a n a n - a n)  #( @ +
   BEGIN  2OVER NIP OVER  >= WHILE   2SWAP DROP	 ,  REPEAT ;
: PRECEDENCE  (	 n)   >IN @  ' >R  >IN !  CONSTANT  R> ,
   IMMEDIATE  DOES>  2@	 DEFER ;
: RPN  ( n)  0 1 DEFER	2DROP  #( @ OR	ABORT" Syntax" ;
: ?IGNORE   #( @  IF  0 1 DEFER	 2DROP	R> DROP	 THEN ;

: NOTHING  ;
: START ( - n)	 0 #( !	 0 ADDRESS !  ['] NOTHING 0
   ARITHMETIC  ;



\ Variables					      06Feb84map
: INTEGER   VARIABLE  IMMEDIATE	 DOES>	[COMPILE] LITERAL
   ADDRESS @ IF	 ADDRESS OFF  ELSE  COMPILE @  THEN ;

: (ARRAY) ( a a)  SWAP >R   7 DEFER  R> [COMPILE] LITERAL
   ADDRESS @ IF ADDRESS OFF ELSE  ['] @	 7 #( @ +  2SWAP  THEN ;

: [+] ( a i - a)   1- 2* + ;
: ARRAY	  INTEGER  1- 2* ALLOT	 DOES>	['] [+] (ARRAY) ;

: [*+] ( a x y - a)   >R  1-  OVER @ *	R> +  2* + ;
: 2ARRAY ( y x)	  DUP CONSTANT	IMMEDIATE  * 2* ALLOT
   DOES>  ['] [*+] (ARRAY) ;



\ Statement numbers  ( works at any address )	      06Feb84map
: FIND ( line# -- entry-adr )	TRUE  #S @ #S 2+
   ?DO	OVER I @ ABS = IF  2DROP I FALSE LEAVE	THEN  4 +LOOP
   IF  0 SWAP  #S @ 2!	#S @  4 #S +!  THEN ;
: RESOLVE ( n -- )   FIND  DUP @  0< ABORT" duplicated"
   DUP @ NEGATE OVER !	 2+ DUP @
   BEGIN  ?DUP	WHILE  DUP @  HERE ROT !  REPEAT  HERE SWAP ! ;

: CHAIN ( n - a)   FIND	 LENGTH 0<
   IF  @   ELSE	 DUP @	HERE ROT !  THEN ;

: STATEMENT ( n -- )   HERE 2- @ >R  -4 ALLOT  RPN EXECUTE
   R> RESOLVE  START ;



\ Branching - high level			      13Mar84map

: JUMP	 R> @ >R ;
: CALL	 R> DUP @ SWAP 2+ >R >R ;
: SKIP	 0= IF	R> 4 + >R  THEN ;
: (NEXT)
   2DUP +!  >R 2DUP R> @ SWAP
   0< IF  SWAP	THEN -
   0< IF  2DROP R> 2+  ELSE  R> @  THEN	 >R ;

: [1]	   COMPILE 1  HERE ;
: [NEXT]   COMPILE (NEXT) , ;
: (GOTO)   GET	COMPILE JUMP  CHAIN , ;
: (RET)	   R> DROP ;


\ BASIC						      19Jul84map
: LET	STATEMENT  ADDRESS ON ; IMMEDIATE
: FOR	[COMPILE] LET ;	 IMMEDIATE
: TO	RPN DROP  ['] [1] 0 ;  IMMEDIATE
: STEP	RPN DROP  ['] HERE 0 ;	IMMEDIATE
: NEXT	STATEMENT 2DROP ['] [NEXT] 0   ADDRESS ON ; IMMEDIATE
: REM	STATEMENT  [COMPILE] \	; IMMEDIATE
: DIM	[COMPILE] REM ; IMMEDIATE
: STOP	STATEMENT  COMPILE (RET) ; IMMEDIATE
: END	STATEMENT 2DROP [COMPILE] ; PREVIOUS FORTH ; IMMEDIATE
: GOTO	 STATEMENT  (GOTO) ;  IMMEDIATE
: IF	 STATEMENT  LOGIC ;  IMMEDIATE
: THEN	 RPN 0	COMPILE SKIP  (GOTO) ; IMMEDIATE
: RETURN   STATEMENT  COMPILE (RET)  ; IMMEDIATE
: GOSUB	 STATEMENT  GET	 COMPILE CALL  CHAIN ,	; IMMEDIATE

\ Input and Output				      06Feb84map
: ASK	." ? "	QUERY ;
: PUT	GET  SWAP ! ;
: (INPUT)   COMPILE PUT ;
: (,) ( n)   (.)  14 OVER - SPACES  TYPE SPACE ;
OUTPUTS DEFINITIONS
: , ( n)   ?IGNORE  ['] (,)  1 DEFER  ;	 IMMEDIATE
: "   [COMPILE] ."  2DROP ;  IMMEDIATE
INPUTS DEFINITIONS
: ,   ?IGNORE  RPN 0  (INPUT)  ADDRESS ON ;  IMMEDIATE

ARITHMETIC DEFINITIONS
: PRINT	  STATEMENT  COMPILE CR	 ['] (,) 1 OUTPUTS ; IMMEDIATE
: INPUT	  STATEMENT  2DROP  COMPILE ASK	 ['] (INPUT) 0	INPUTS
   ADDRESS ON ;	 IMMEDIATE

\ Operators					      06Feb84map
LOGIC DEFINITIONS
2 PRECEDENCE <>	  2 PRECEDENCE <=   2 PRECEDENCE >=
2 PRECEDENCE =	  2 PRECEDENCE <    2 PRECEDENCE >

ARITHMETIC DEFINITIONS
: = ( a n)   SWAP ! ;	1 PRECEDENCE =
: ** ( n n - n)	  1  SWAP 1 DO	OVER * LOOP  * ;
6 PRECEDENCE ABS
5 PRECEDENCE **
4 PRECEDENCE *	 4 PRECEDENCE /	  4 PRECEDENCE */
3 PRECEDENCE +	 3 PRECEDENCE -




\ [ Dwyer, page 17, Program 1]	 ( works )	      06Feb84map
INTEGER J   INTEGER K

: RUN	BASIC
10 PRINT " THIS IS A COMPUTER"
20 FOR K = 1 TO 4
30 PRINT " NOTHING CAN GO"
40 FOR J = 1 TO 3
50 PRINT " WRONG"
60 NEXT J
70 NEXT K
80 END

RUN


\ [ basic: branching demo ]   ( works )		      06Feb84map
INTEGER J   INTEGER K

: RUN	BASIC
10 FOR K = 1 TO 15 STEP 3
15 LET J = J + K
20 IF K >= 8 THEN 35
25 PRINT K
30 GOTO 40
35 PRINT K , J , " SUM "
40 NEXT K
50 PRINT " DONE	 "
80 END

RUN

\ [ basic: array demo ]	 ( works )		      06Feb84map
INTEGER K   9 ARRAY COORDINATE

: RUN	BASIC
10 FOR K = 1 TO 9
20 LET COORDINATE K = ( 10 - K ) ** 3
40 PRINT COORDINATE K + 5
60 NEXT K
80 END

RUN





\  [ basic string printing demo ]		      06Feb84map
INTEGER X  INTEGER Y  INTEGER Z

: RUN	BASIC
10 LET X = 5
20 LET Y = 7
30 PRINT  X ,	Y
60 PRINT X , " TEST "
90 END

RUN





\ [ basic program # 1 ]	 ( works )		      06Feb84map
INTEGER K  INTEGER X  3 ARRAY Z

: RUN	BASIC
10 LET Z 1 = 1
15 LET Z 2 = 22
20 LET Z 3 = 333
30 FOR K = 1 TO 3
40 LET X = Z K
50 PRINT X
60 NEXT K
80 END

RUN


\ [ basic inputting demo ]			      06Feb84map
INTEGER K  INTEGER X  INTEGER Y

: RUN	BASIC
10 INPUT X , Y
20 LET K = X * Y ** 3
40 PRINT X , Y , K
80 END

RUN






\ [ basic: GOSUB demo ]				      19Jul84map
INTEGER K
9 ARRAY COORDINATE

: RUN	BASIC
10 FOR K = 1 TO 9
20 LET COORDINATE K = 10 - K
30 GOSUB 60
40 NEXT K
50 GOTO 80
60 PRINT COORDINATE K
70 RETURN
80 END

Last edited by schmitta; 11-20-2014 at 12:05 AM.
 
Old 11-19-2014, 11:32 PM   #306
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,604

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
I think that is the smallest BASIC I have ever seen.

About the only statement that appears missing is a "DATA", along with read to scan the list of values in the data statement.
 
Old 11-20-2014, 03:02 AM   #307
maaazkhan
LQ Newbie
 
Registered: Nov 2014
Posts: 3

Rep: Reputation: Disabled
i appreciate the effort you took for this ! thumbs up
 
Old 01-29-2015, 04:13 AM   #308
schmitta
Member
 
Registered: May 2011
Location: Blacksburg VA
Distribution: UBUNTU, LXLE
Posts: 308

Original Poster
Rep: Reputation: Disabled
I am a java newbie. I need to have a list of commands and a way to vector through the list to find out what command was entered by the user. In C I would have an array of structures with each structure have the ascii command and a number to be associated with the command to be used by a switch statement to execute the entered command. I guess I have to use a class in java but I don't know how to have an array of ascii commands and the associated number of the command. Any ideas would be greatly appreciated. Also I need some form of #define to relate a defined name to a number.

Last edited by schmitta; 01-29-2015 at 04:15 AM.
 
Old 01-29-2015, 06:48 AM   #309
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,604

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
Can't help much with Java itself (my last use was almost 15 years ago, and then only because my wife took a class). As I recall, everything was a "class", even the main program.

Procedurally, it would be very similar to C - but the syntax is quite different as Java doesn't permit simple pointers.

You might try the answer from:

http://stackoverflow.com/questions/1...bjects-in-java

But I don't think it will be quite the same. As one of the posters there indicated arrays can't really be static, they still get created at run time.

side note: I really wasn't that impressed with the language when it came out. I thought there was too much lost in the "make it safe" bandwagon - and it made some things ether very hard to do, or very slow.

This might make a good question for a new thread though. There will be others that use Java on a regular basis.

Last edited by jpollard; 01-29-2015 at 06:56 AM.
 
1 members found this post helpful.
Old 10-15-2015, 01:55 AM   #310
schmitta
Member
 
Registered: May 2011
Location: Blacksburg VA
Distribution: UBUNTU, LXLE
Posts: 308

Original Poster
Rep: Reputation: Disabled
Hello Again

Long time no communicate. Written any languages lately? I feel the urge to get back into my BASIC interpreter project. Before, I modified a parser and wrote mostly a brute force interpreter which I would like to continue but now feel the urge to use flex and bison and have the BNF of the commodore 64 BASIC interpreter. I would like to add while/wend, do/loop, etc. to it to give it block statement and to extend the IF statement beyond one line. When you used flex and bison you used triples to produce code and as part of the BNF expansion which I never fully understood. I am not tremendously smart but persistent and know that triples were a foundation to your coding repertoire and I was wondering if you would be so kind as to explain how they work and your use with possibly an example from your code. Maybe you will catch the bug and get back into your C compiler as well. Thanks and happy coding.
 
Old 10-15-2015, 04:58 AM   #311
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,604

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
Not much activity - ran out of steam and started playing games, reading news... Upgraded my system to Fedora 21, Got a birthday present- a Raspberry Pi 2. Mostly turned it into an entertainment system (quad ARM, NFS mounted from our main system, and built a library, not bad for $70 - and I haven't used all of the kit yet). Unfortunately, can't play the free hulu videos (they still require flash).

I used triples because that is how rules get made - they combine two pieces of information to define a third.

It is possible to use more, but it makes the interpretation a bit trickier as then you have to know if a given rule has two or three elements. Two is enough, when one of them refers to a rule for two. Thus the "if":

a triple- if : condition true_part
a triple- true_part: then statement_block
a triple- statement_block: statements end_block

Yet a quad is also good for if:

quad- if: condition true_part false_part

Which works for the "if" when the "false_part" is allowed to be empty. Makes the rule easier, but it can also be done with triples:

a triple- if: condition test
a triple- test: true_part false_part
a triple- true_part: then statement_block
a triple- false_part: else statement_block | (null statement)

again, with "false_part" allowed to be empty. The advantage is that more context is available for errors. Correctly parsing "condition", allows for correctly parsing test. If an error occurs in the true part (such as mispelled "then") it is easy to have an appropriate error message (missing then), and can be treated as if it exists (or spell check the symbol when it occurs to see if it is missing or not; usually just reporting an error with "then" and the line/column is enough - and that means the scanner has to track line/column).

Quads in parsing are easier to use for recognition, but the parse tree remains triples (the literals used don't appear in the tree):

statement_block: statement | block_begin statement_list block_end

Here, block_begin might be defined as "{", and block_end might be "}". The end result is that recognition allows
for a "statement_block" to be "statement" or "statement_list" (which actually is a double rather than triple in the tree).

All it needs is for the tree to be able to express the parse effectively, whether it allows double, or triples. The structure of a triple only has two elements. a structure for a double, only has one element; but can be represented using a triple with a null (but be consistent as to which is null... it helps debugging).

Recursive decent parsing can make some things easier, though it results in more code. One of the things easier is error handling - it is easier to generate error messages; but a bit harder to identify recovery because you have to unwind the stack, and skip things until you reach a valid location. (in an expression, the end of the expression...) The earliest C compilers just aborted after the first error. Later ones tried to find a restart location - but that nearly always causes cascading errors (error in a struct definition prevents it from being defined; thus every reference to that structure also generates errors... fixing one error can fix dozens of errors... The nature of the recursive decent compilers is that more context information can be available for recovery (recognizing a missing ";" allows the translator to insert it, and continue...).

One of the first compilers I worked with for FORTRAN handled things similarly: it always would try to parse a line as being an assignment. If that got an error, then it would try parsing for a keyword instead. Only if all parses failed would it generate an error... It made for slow compiling as it had to rescan the line each time, but did mean you could have an array/variable named "IF". So if the assignment got an error, then it tried the keyword "IF"... (mostly if it didn't find an "=" for an assignment the line had to start with a keyword...)

Bison produces LR(1) grammar parsers - which means it can only look at one symbol being input, and the stack. Works for many computer languages - but not all (FORTRAN is not LR(1) for instance, nor is Bash/sh). Bison does have some extensions to help but I'm not that familiar with them.
 
1 members found this post helpful.
Old 10-15-2015, 03:49 PM   #312
schmitta
Member
 
Registered: May 2011
Location: Blacksburg VA
Distribution: UBUNTU, LXLE
Posts: 308

Original Poster
Rep: Reputation: Disabled
Thank you for a very through explanation. I will do further research on google. Enjoy your PI. I got one and built a table to hold all the required equipment using Kreg Joints.
 
Old 10-21-2015, 02:52 AM   #313
schmitta
Member
 
Registered: May 2011
Location: Blacksburg VA
Distribution: UBUNTU, LXLE
Posts: 308

Original Poster
Rep: Reputation: Disabled
With this second approach to the BASIC interpreter I am considering a compiler to produce an intermediate language which would be interpreted. It would seem vile to me to reinterpret each expression and command when they could be generated from the first time through the program as an intermediate or byte code. Also it would make the software portable just writing a new interpreter for the intermediate language. A stack language with a simplified forth may be one way to do it. Either producing quads or something like your stack assembly language. Would you be willing to let me use your assembly language? If I end up profiting from it I could give you a percentage of the income for each unit sold. You would get something back for all the work you put into your project.
 
Old 10-30-2015, 09:55 PM   #314
schmitta
Member
 
Registered: May 2011
Location: Blacksburg VA
Distribution: UBUNTU, LXLE
Posts: 308

Original Poster
Rep: Reputation: Disabled
You might find this interesting: https://unnikked.ga/how-to-build-sim...rtual-machine/

http://yosefk.com/blog/my-history-wi...-machines.html

Last edited by schmitta; 10-30-2015 at 10:40 PM.
 
Old 10-31-2015, 03:13 AM   #315
schmitta
Member
 
Registered: May 2011
Location: Blacksburg VA
Distribution: UBUNTU, LXLE
Posts: 308

Original Poster
Rep: Reputation: Disabled
You gave me the .c files for your stack machine and all the associated files. Mind if I use them in products I sell?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Regular Expressions nova49 Linux - Newbie 4 07-13-2011 08:05 AM
Regular Expressions Wim Sturkenboom Programming 10 11-19-2009 02:21 AM
regular expressions. stomach Linux - Software 1 02-10-2006 07:41 AM
Regular Expressions overbored Linux - Software 3 06-24-2004 03:34 PM
help with REGULAR EXPRESSIONS ner Linux - General 23 11-01-2003 12:09 AM


All times are GMT -5. The time now is 04:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration