Operator Counter
#21
Hey Codeguy, I never even thought about implied operations, nice to see that.  I tried running your snippet and got an error but it sounds like a work in progress, which pretty much everything I do is too, nothing ever gets completely done, just done enough to work.
Reply
#22
More edits to Phred's recursive descent parser:

Code:
'**********************************
'a simple recursive descent parser
'based on Jack Crenshaws tutorial
'**********************************

' edited by phred
' edited by ejo to add more descent tokens

CONST FALSE = 0
CONST TRUE = NOT FALSE
PRINT "Enter equation: includes +-/*^!<>=()"
DO
    PRINT "Solve";: INPUT S$
    IF S$ = "" THEN END
    PRINT solve(S$)
LOOP
END

FUNCTION solve (eval$)
'begin evaluation
getchar eval$, char$
isexpr eval$, char$, value
solve = value
END FUNCTION

SUB isexpr (eval$, char$, value)
'is it a valid term?
isterm0 eval$, char$, value
WHILE INSTR("<>=", char$)
    op$ = char$
    getchar eval$, char$
    isterm0 eval$, char$, value2

    IF op$ = "<" THEN
        value = value < value2
        PRINT "IS EXPRESSION "; "<"
    ELSE
        IF op$ = ">" THEN
            value = value > value2
            PRINT "IS EXPRESSION "; ">"
        ELSE
            value = value = value2
            PRINT "IS EXPRESSION "; "="
        END IF
    END IF
WEND
END SUB

SUB isterm0 (eval$, char$, value)
'is it a valid factor?
isterm1 eval$, char$, value
WHILE INSTR("+-", char$)
    op$ = char$
    getchar eval$, char$
    isterm1 eval$, char$, value2

    IF op$ = "+" THEN
        value = value + value2
        PRINT "IS TERM "; "+"
    ELSE
        value = value - value2
        PRINT "IS TERM "; "-"
    END IF
WEND
END SUB

SUB isterm1 (eval$, char$, value)
'is it a valid factor?
isterm2 eval$, char$, value
WHILE INSTR("*/", char$)
    op$ = char$
    getchar eval$, char$
    isterm2 eval$, char$, value2

    IF op$ = "*" THEN
        value = value * value2
        PRINT "IS TERM "; "*"
    ELSE
        value = value / value2
        PRINT "IS TERM "; "/"
    END IF
WEND
END SUB

SUB isterm2 (eval$, char$, value)
'is it a valid factor?
isterm3 eval$, char$, value
WHILE INSTR("^", char$)
    op$ = char$
    getchar eval$, char$
    isfactor eval$, char$, value2

    IF op$ = "^" THEN
        value = value ^ value2
        PRINT "IS TERM "; "^"
    END IF
WEND
END SUB

SUB isterm3 (eval$, char$, value)
'is it a valid factor?
isfactor eval$, char$, value
WHILE INSTR("!", char$)
    op$ = char$
    getchar eval$, char$
    isfactor eval$, char$, value2

    IF op$ = "!" THEN
        value = NOT value2
        PRINT "IS TERM "; "!"
    END IF
WEND
END SUB

SUB isfactor (eval$, char$, value)
'is it a partial formula in braces?
IF char$ = "(" THEN
    getchar eval$, char$
    isexpr eval$, char$, value
    getchar eval$, char$
ELSE
    getdigits eval$, char$, value
END IF
END SUB

SUB getchar (eval$, char$)
eval$ = LTRIM$(eval$)
IF eval$ <> "" THEN
    'if there's a string grab the first character
    char$ = LEFT$(eval$, 1)
    'and remove it from the sequence
    eval$ = MID$(eval$, 2)
ELSE
    char$ = " "
END IF
END SUB

SUB getdigits (eval$, char$, value)
IF eval$ <> "" THEN
    'while the characters are numbers collect them
    WHILE isdigit(char$)
        number$ = number$ + char$
        getchar eval$, char$
    WEND
ELSE
    number$ = char$
END IF
value = VAL(number$)
END SUB

FUNCTION isdigit (char$)
isdigit = FALSE
IF char$ >= "0" AND char$ <= "9" OR char$ = "." THEN isdigit = TRUE
END FUNCTION
dndbbs project:

Links to my MUD: (strictly 16-bit); AKA XP:

Dndbbs executables
http://www.filegate.net/pdn/pdnbasic/dnd50a1e.zip

Dndbbs source
http://www.filegate.net/pdn/pdnbasic/dnd50a1s.zip

Dndbbs upgrade
http://www.filegate.net/pdn/pdnbasic/dnd50a1u.zip

DNDDOOR - https://bit.ly/EriksDNDDoor DUNGEON - https://bit.ly/EriksDungeon
Interpreter - https://bit.ly/EriksSICK Hex Editor - https://bit.ly/EriksHexEditor Utilities - https://bit.ly/EriksUtils
QB45 files: - https://bit.ly/EriksQB45 QB64shell - https://bit.ly/QB64shell Some old QB64 versions: - https://bit.ly/OldQB64
Reply