"Untitled Post" - Views: 274 · Hits: 274 - Type: Public

"" "By Burke Johnson" "Micro Script RPN Calculator":\ :\ :\ main

{				;calc function, evaluates an expression

	parse			;parse/tokenize the input
	0:i			;initialize the counter
	#			;clear the stack
	
	{			;calculation loop (this is where it solves the expression)
		i 1+:1		;increment i
		p p
		i tokens m :token	;find current token
		p p
		0:istoken	;initialize istoken (shows if the current token is a builtin token)
		p
		"+" token = if(pushtoken,)
		p p p
		"-" token = if(pushtoken,)
		p p p
		"*" token = if(pushtoken,)
		p p p
		"/" token = if(pushtoken,)
		p p p
		istoken if(,pushnumber)
		p
	calcloop}:calcloop
}:calc


{				;parsing routine

	"  "+:in		;adds 2 spaces to the end of the input, so that the tokenizer knows where to stop
	[]:tokens		;creates an array that will store the tokens once they have been extracted
	"":token		;initializes a string where a token will be stored, while the program is looking for the ending space
	0:i			;creates the counter variable
	0:ls			;this variable remembers of the previous character was a space

	{			;loop function, holds the token finding code
	i 1+:i			;increment i
	i in m:c		;stores current char in a variable names c
	#			;clears all unused junk out of the stack
	
	c " " = ! if(newchar,clear_routine)	;decide if a newchar needs to be added to the token, or if the token is done.
	
	c " " = ls & if(,parse)	;end the loop if the current, and previous chars were spaces, else continue
}:parse
{token SB :function p function}:pushtoken	;push a token as a string, convert it to a block, store the block to a function, and run it
{token SN}:pushnumber				;push number as a string, convert it to a number
{ls if(,clear)}:clear_routine			;if the previous char wasnt a space, run clear, else do nothing
{tokens token + "":c}:clear			;clear the token variable, and move the token to the 'tokens' token list
{token c +}:newchar				;add a new character to the token
{/: calc :/ main}:main				;main loop