@TheMagicNumber
Heh, your dll would obviously be faster since it is coded in C++, but here's how it might look in GML (GM6). Just for laffs!
Place the code in the creation even of an object and put it in a room.
font0 properties are :
Font : Arial
Size 10
Character Range : Normal 32 till 127
/*
function : tokenizes an expression and stores the tokens in an array
An expression may contain :
numbers, (, ), *, /, +, -, %, "string",variable names,
arrays,commas,function names
parentheses are used by functions, arrays, and grouping sub-expressions
* is multiplication
/ is division
+ is addition
- is subtraction
% is modulo
A string is encapsulated in double quotation marks '"' or single quotation
marks "'" at the beginning and end of the string. A string enclosed in double
quotation marks cannot have double quotation marks inside, single quotation
marks should be used.
Example of illegal string :
"this is "a" string"
Example of illegal string :
'this is 'a' string'
Example of legal string :
"this is a string"
Example of legal string :
'this is "a" string'
Function names, variable names and array names must start with an underscore
'_' chr(95) or an uppercase letter chr(65) to chr(90) or a lowercase letter
chr(97) to chr(122) followed by more uppercase letters, lowercase letters,
underscores, or numbers chr(48) to chr(57)
For numbers we look for ascii codes chr(48) to chr(57)
Note : numbers may be a float and have a decimal point so chr(46) should
be taken into account
All our tokens are stored as strings. To indicate what type of token
is stored we prefix each token with a code :
0 indicates a string value example : "text" is "0text"
1 indicates a real value example : 58.4 is "158.4"
2 indicates a variable name, or a function name, or an array name
example : name1 is "2name1"
3 indicates operators, parentheses and commas (argument separators)
example : ( is "3("
another example : + is "3+"
*/
expression = 'function1(var1,var2,var3)+45.899-32+a/function2("a string")%(-82 - 5)* -75'
max_index = -1
single_char = "()*/+-%,"
number_char = "0123456789."
name_char1 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_"
name_char2 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_"
while (string_length(expression) > 0)
{
char = string_char_at(expression,1)
//error checking
if string_count(char,single_char) = 0
{
if string_count(char,number_char) = 0
{
if string_count(char,name_char2) = 0
{
if char != " "
{
if char != '"'
{
if char != "'"
{
show_message("Error : Expression contains illegal characters.")
exit
}
}
}
}
}
}
//if it is a single token ()*/+-%,
if string_count(char,single_char) > 0
{
max_index += 1
token_array[max_index] = "3" + char
expression = string_delete(expression,1,1)
}
//ignore space, do nothing
if char = " "
{
expression = string_delete(expression,1,1)
}
//if a string using double quotes is found
if char = '"'
{
pos = 1
do
{
pos += 1
if pos > string_length(expression)
{
show_message("Error : Illegal string.")
exit
}
next_char = string_char_at(expression,pos)
}
until (next_char = '"')
temp1 = string_copy(expression,1,pos)
temp1 = string_replace_all(temp1,'"',"")
max_index += 1
token_array[max_index] = "0" + temp1
expression = string_delete(expression,1,pos)
}
//if a string using single quotation marks
if char = "'"
{
pos = 1
do
{
pos += 1
if pos > string_length(expression)
{
show_message("Error : Illegal string.")
exit
}
next_char = string_char_at(expression,pos)
}
until (next_char = "'")
temp1 = string_copy(expression,1,pos)
temp1 = string_replace_all(temp1,"'","")
max_index += 1
token_array[max_index] = "0" + temp1
expression = string_delete(expression,1,pos)
}
//if it is a number
if string_count(char,number_char) > 0
{
pos = 1
do
{
pos += 1
if pos > string_length(expression)
{
pos -= 1
break
}
next_char = string_char_at(expression,pos)
}
until (string_count(next_char,number_char) = 0)
if string_count(next_char,number_char) > 0
{
temp1 = string_copy(expression,1,pos)
expression = string_delete(expression,1,pos)
}
else
{
temp1 = string_copy(expression,1,pos-1)
expression = string_delete(expression,1,pos-1)
}
if string_count(".",temp1) > 1
{
show_message("Error : Illegal real number.")
exit
}
max_index += 1
token_array[max_index] = "1" + temp1
}
//if it is a function/variable/array name
if string_count(char,name_char1) > 0
{
pos = 1
do
{
pos += 1
if pos > string_length(expression)
{
pos -= 1
break
}
next_char = string_char_at(expression,pos)
}
until (string_count(next_char,name_char2) = 0)
if string_count(next_char,name_char2) > 0
{
temp1 = string_copy(expression,1,pos)
expression = string_delete(expression,1,pos)
}
else
{
temp1 = string_copy(expression,1,pos-1)
expression = string_delete(expression,1,pos-1)
}
max_index += 1
token_array[max_index] = "2" + temp1
}
}
//debugging
temp1 = 'function1(var1,var2,var3)+45.899-32+a/function2("a string")%(-82 - 5)* -75' + '#'
for (count = 0; count <= max_index; count += 1)
{
temp1 += token_array[count] + " "
}
draw_clear(c_black)
draw_set_font(font0)
draw_set_color(c_white)
draw_text_ext(0,0,temp1,-1,600)
set_automatic_draw(0)
Pretty neat dll. Too bad I can't find any concrete info on how to convert an infix expression to a postfix expression taking into account
functions,
variables and
arrays. Then evaluate that postifx expression as a result. If I knew that I'm pretty sure I could make an interpreted language.
The Wikipedia articles only hint on how to go about doing this :
http://en.wikipedia....-yard_algorithmhttp://en.wikipedia....Polish_notation
Edited by attm, 19 September 2010 - 05:14 AM.