Usage
Usage
To use Thida Lang, you will need to implement a lexer, parser, and execution environment in Python. This section provides an overview of how to get started with Thida Lang by creating these components in Python. By following these steps, you can start writing and executing Thida Lang code.
Installation Setup
Install Python: Ensure you have Python installed on your system. You can download it from Python's official website.
Set Up Project Directory: Create a project directory for your Thida Lang implementation.
mkdir thida_lang cd thida_lang
Create Virtual Environment: It is recommended to create a virtual environment for your project.
python -m venv venv source venv/bin/activate # On Windows use `venv\Scripts\activate`
Install Required Packages: Install any required Python packages. For example, you might need
ply
for lexer and parser implementations.pip install ply
Create Thida Lang Files: Create the necessary files for your lexer, parser, and execution environment.
touch lexer.py parser.py interpreter.py main.py
Implement Lexer: In
lexer.py
, implement the lexer to tokenize Thida Lang code.# lexer.py import ply.lex as lex tokens = ( 'NUMBER', 'PLUS', 'MINUS', 'TIMES', 'DIVIDE', 'LPAREN', 'RPAREN', 'EQUALS', 'STRING', 'BOOLEAN', 'IDENTIFIER' ) t_PLUS = r'\+' t_MINUS = r'-' t_TIMES = r'\*' t_DIVIDE = r'/' t_LPAREN = r'\(' t_RPAREN = r'\)' t_EQUALS = r'=' t_ignore = ' \t' def t_NUMBER(t): r'\d+' t.value = int(t.value) return t def t_STRING(t): r'\".*?\"' t.value = t.value.strip('"') return t def t_BOOLEAN(t): r'ဖြစ်သည်|မဖြစ်ပါ' t.value = t.value == 'ဖြစ်သည်' return t def t_IDENTIFIER(t): r'[a-zA-Z_][a-zA-Z0-9_]*' return t def t_newline(t): r'\n+' t.lexer.lineno += len(t.value) def t_error(t): print(f"Illegal character '{t.value[0]}'") t.lexer.skip(1) lexer = lex.lex()
Implement Parser: In
parser.py
, implement the parser to generate a syntax tree from the tokens.python # parser.py import ply.yacc as yacc from lexer import tokens def p_statement_expr(p): 'statement : expression' print(p[1]) def p_expression_binop(p): '''expression : expression PLUS expression | expression MINUS expression | expression TIMES expression | expression DIVIDE expression''' if p[2] == '+': p[0] = p[1] + p[3] elif p[2] == '-': p[0] = p[1] - p[3] elif p[2] == '*': p[0] = p[1] * p[3] elif p[2] == '/': p[0] = p[1] / p[3] def p_expression_number(p): 'expression : NUMBER' p[0] = p[1] def p_expression_string(p): 'expression : STRING' p[0] = p[1] def p_expression_boolean(p): 'expression : BOOLEAN' p[0] = p[1] def p_expression_identifier(p): 'expression : IDENTIFIER' p[0] = p[1] def p_error(p): print(f"Syntax error at '{p.value}'") parser = yacc.yacc()
Implement Interpreter: In
interpreter.py
, write the code to execute the parsed syntax tree.pythonCopy code# interpreter.py # Simple interpreter logic, for example, could go here
Run Thida Lang Code: Create a main script to run Thida Lang code.
python # main.py from lexer import lexer from parser import parser def run_thida_lang_code(code): lexer.input(code) for token in lexer: print(token) parser.parse(code) if __name__ == "__main__": code = ''' ( a = 10 ၊ b = 20 ) ဟု ယူဆပါ။ ( a နဲ့ b ကို ပေါင်းပါ။ ) ကို အဖြေထုတ်ပါ။ ''' run_thida_lang_code(code)
By following these steps, you can set up the Thida Lang programming environment and start writing and executing Thida Lang code.
Last updated