├── LICENSE ├── README.md ├── SourceCode ├── Parser │ ├── Lexer.cs │ ├── Parser.cs │ ├── Token.cs │ └── TokenType.cs ├── ast │ ├── ArrayAccesExpression.cs │ ├── ArrayAssignmentStatement.cs │ ├── AssignmentStatement.cs │ ├── BinaryExpression.cs │ ├── BlockStatement.cs │ ├── BreakStatement.cs │ ├── CaseStatement.cs │ ├── ConditionalExpression.cs │ ├── ConstantExpression.cs │ ├── ContinueStatement.cs │ ├── DdotEqAssignmentStatement.cs │ ├── DdotExpression.cs │ ├── DoWhileStatement.cs │ ├── Expression.cs │ ├── ForStatement.cs │ ├── FunctionDefine.cs │ ├── FunctionStatement.cs │ ├── FunctionalExpression.cs │ ├── IfStatement.cs │ ├── LambdaExpression.cs │ ├── ObjectAssignmentStatement.cs │ ├── PrintStatement.cs │ ├── ReturnStatement.cs │ ├── Statement.cs │ ├── StatementStatement.cs │ ├── SwitchStatement.cs │ ├── TryCatchStatement.cs │ ├── UnaryExpression.cs │ ├── ValueExpression.cs │ ├── WhileStatement.cs │ └── lib │ │ ├── AsyncFunctionStatement.cs │ │ ├── Class │ │ ├── Class.cs │ │ ├── ClassDefine.cs │ │ ├── ClassExpression.cs │ │ ├── ClassStatement.cs │ │ ├── Classes.cs │ │ └── UserDefineClass.cs │ │ ├── CommandLineArgs.cs │ │ ├── ConstStatement.cs │ │ ├── Constants.cs │ │ ├── Dictionary │ │ ├── DictionaryAccesExpression.cs │ │ └── DictionaryAssignmentStatement.cs │ │ ├── DogExpression.cs │ │ ├── EqconExpression.cs │ │ ├── ForeachStatement.cs │ │ ├── Function.cs │ │ ├── Functions.cs │ │ ├── IfThenElseExpression.cs │ │ ├── ImportStatement.cs │ │ ├── InstStatement.cs │ │ ├── Ldef.cs │ │ ├── LdefExpression.cs │ │ ├── Marks.cs │ │ ├── NanStatement.cs │ │ ├── NewAssignmentStatement.cs │ │ ├── Stack │ │ ├── StackAccesExpression.cs │ │ └── StackAssignmentStatement.cs │ │ ├── Starcon.cs │ │ ├── SuffixAssignmentStatement.cs │ │ ├── SuffixExpression.cs │ │ ├── UseStatement.cs │ │ ├── UserDefineFunction.cs │ │ ├── UsingStatement.cs │ │ ├── Value │ │ ├── ArrayValue.cs │ │ ├── BoolValue.cs │ │ ├── ByteValue.cs │ │ ├── DictionaryValue.cs │ │ ├── EnumValue.cs │ │ ├── NumberValue.cs │ │ ├── ObjectValue.cs │ │ ├── StackValue.cs │ │ ├── StatementValue.cs │ │ ├── StringValue.cs │ │ └── Value.cs │ │ └── Variables.cs └── program │ └── Program.cs ├── examples ├── class_example.gs ├── client.gs ├── command_lang.cl ├── command_lang.gs ├── functional.gs ├── game.gs ├── jscalc.gs ├── newfeaturestest.gs ├── object_inst.gs ├── preprocessor.gs ├── server.gs ├── simple_calculator.gs └── socket_web_server.gs └── gs.png /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 bas1c 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 |
3 |

4 | GoScript 5 |

6 | 7 |

Go Script Programming Language

8 | 9 | Go Script is a dynamically typed, general purpose programming language for creating universal applications! 10 | 11 |
12 | 13 |

Features

14 | 15 | - The possibility of functional and object-oriented programming 16 | - Knife http listening library 17 | - Supports the ability to integrate JS code 18 | - Friendly and easy-to-read syntax 19 | - Lightweight interpreter 20 | - Standart libraries 21 | 22 |

In the last update

23 | 24 | In the latest update was: 25 | 26 | - updated "files" library 27 | - added new data type "byte" 28 | - added command line arguments 29 | - added "const" operator 30 | - added "using" statement 31 | - added "if-then-else" expression 32 | - added new "socket" library 33 | 34 | `~7000` lines of code 35 | 36 |
37 |

Documentation

38 | Documentation 39 |
40 | 41 |
42 |

Contribution

43 | Go Script is open-source project. You can throw pull requests and I'll look through them all! 44 |
45 | 46 |
47 |

DevKit

48 | DevKit 49 |
50 | 51 |

Examples

52 | 53 | (Very simple blockchain written on GoScript) 54 | 55 | (More examples) 56 | 57 | Simple "Hello world": 58 | 59 | ```py 60 | use "std" 61 | 62 | print("Hello, world!") 63 | 64 | stop() 65 | 66 | //Output: Hello, world! 67 | ``` 68 | -------------------------------------------------------------------------------- /SourceCode/Parser/Lexer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang 8 | { 9 | public class Lexer 10 | { 11 | private static string OPERATOR_CHARS = "+-*/()[]{}=<>!&|;.:@"; 12 | private string input; 13 | private List tokens; 14 | private static Dictionary OPERATORS; 15 | static Lexer() 16 | { 17 | OPERATORS = new Dictionary(); 18 | 19 | OPERATORS.Add("++", TokenType.INC); 20 | OPERATORS.Add("--", TokenType.DEC); 21 | 22 | OPERATORS.Add("+=", TokenType.PLUSEQ); 23 | OPERATORS.Add("-=", TokenType.MINUSEQ); 24 | OPERATORS.Add("*=", TokenType.STAREQ); 25 | OPERATORS.Add("/=", TokenType.SLASHEQ); 26 | 27 | OPERATORS.Add("%", TokenType.PERCENT); 28 | OPERATORS.Add("**", TokenType.STARSTAR); 29 | 30 | OPERATORS.Add("+", TokenType.PLUS); 31 | OPERATORS.Add("-", TokenType.MINUS); 32 | OPERATORS.Add("*", TokenType.STAR); 33 | OPERATORS.Add("/", TokenType.SLASH); 34 | 35 | OPERATORS.Add("(", TokenType.LPAREN); 36 | OPERATORS.Add(")", TokenType.RPAREN); 37 | OPERATORS.Add("[", TokenType.LBRAСKET); 38 | OPERATORS.Add("]", TokenType.RBRACKET); 39 | OPERATORS.Add("{", TokenType.LBRACE); 40 | OPERATORS.Add("}", TokenType.RBRACE); 41 | OPERATORS.Add("=", TokenType.EQ); 42 | OPERATORS.Add("<", TokenType.LT); 43 | OPERATORS.Add(">", TokenType.GT); 44 | OPERATORS.Add(";", TokenType.COMMA); 45 | OPERATORS.Add(".", TokenType.DOT); 46 | OPERATORS.Add(":", TokenType.DDOT); 47 | OPERATORS.Add(":=", TokenType.DDOTEQ); 48 | OPERATORS.Add("=>", TokenType.EQCON); 49 | OPERATORS.Add("*>", TokenType.STARCON); 50 | OPERATORS.Add("@", TokenType.DOG); 51 | 52 | OPERATORS.Add("!", TokenType.EXCL); 53 | OPERATORS.Add("&", TokenType.AMP); 54 | OPERATORS.Add("|", TokenType.BAR); 55 | OPERATORS.Add("^", TokenType.XOR); 56 | 57 | OPERATORS.Add("==", TokenType.EQEQ); 58 | OPERATORS.Add("!=", TokenType.EXCLEQ); 59 | OPERATORS.Add("<=", TokenType.LTEQ); 60 | OPERATORS.Add(">=", TokenType.GTEQ); 61 | 62 | OPERATORS.Add("&&", TokenType.AMPAMP); 63 | OPERATORS.Add("||", TokenType.BARBAR); 64 | } 65 | private int pos; 66 | private int length; 67 | public Lexer(string input) 68 | { 69 | this.input = input; 70 | length = input.Length; 71 | 72 | tokens = new List(); 73 | } 74 | 75 | public List tokenize() 76 | { 77 | while (pos < length) 78 | { 79 | char current = peek(0); 80 | if (Char.IsDigit(current)) tokenizeNumber(); 81 | else if (Char.IsLetter(current) || current=='_' || current=='#') tokenizeWord(); 82 | else if (OPERATOR_CHARS.IndexOf(current) != -1) 83 | { 84 | tokenizeOperator(); 85 | } 86 | else if (current == '"') 87 | { 88 | tokenizeText(); 89 | } 90 | else 91 | { 92 | next(); 93 | } 94 | } 95 | return tokens; 96 | } 97 | 98 | /*public Token tokenizeSmth(string str) 99 | { 100 | char current = peek(0); 101 | if (Char.IsDigit(current)) tokenizeNumber(); 102 | else if (Char.IsLetter(current) || current == '_' || current == '#') tokenizeWord(); 103 | else if (OPERATOR_CHARS.IndexOf(current) != -1) 104 | { 105 | tokenizeOperator(); 106 | } 107 | else if (current == '"') 108 | { 109 | tokenizeText(); 110 | } 111 | Token token = tokens[tokens.Count - 1]; 112 | tokens.RemoveAt(tokens.Count - 1); 113 | return token; 114 | }*/ 115 | 116 | private void tokenizeText() 117 | { 118 | next(); 119 | StringBuilder buffer = new StringBuilder(); 120 | char current = peek(0); 121 | while (true) 122 | { 123 | if (current == '\\') 124 | { 125 | current = next(); 126 | switch (current) 127 | { 128 | case '"': 129 | { 130 | current = next(); 131 | buffer.Append('"'); 132 | continue; 133 | } 134 | case 'n': 135 | { 136 | current = next(); 137 | buffer.Append('\n'); 138 | continue; 139 | } 140 | case 't': 141 | { 142 | current = next(); 143 | buffer.Append('\t'); 144 | continue; 145 | } 146 | case 'b': 147 | { 148 | current = next(); 149 | buffer.Append('\b'); 150 | continue; 151 | } 152 | case 'r': 153 | { 154 | current = next(); 155 | buffer.Append('\r'); 156 | continue; 157 | } 158 | case 'a': 159 | { 160 | current = next(); 161 | buffer.Append('\a'); 162 | continue; 163 | } 164 | case 'f': 165 | { 166 | current = next(); 167 | buffer.Append('\f'); 168 | continue; 169 | } 170 | case 'v': 171 | { 172 | current = next(); 173 | buffer.Append('\v'); 174 | continue; 175 | } 176 | } 177 | buffer.Append('\\'); 178 | continue; 179 | } 180 | if (current == '"') 181 | { 182 | break; 183 | } 184 | buffer.Append(current); 185 | current = next(); 186 | } 187 | next(); 188 | 189 | addToken(TokenType.TEXT, buffer.ToString()); 190 | } 191 | 192 | private void tokenizeWord() 193 | { 194 | StringBuilder buffer = new StringBuilder(); 195 | char current = peek(0); 196 | while (true) 197 | { 198 | if (!Char.IsLetterOrDigit(current) && (current != '_') && (current != '$') && (current != '#')) 199 | { 200 | break; 201 | } 202 | buffer.Append(current); 203 | current = next(); 204 | } 205 | string word = buffer.ToString(); 206 | switch (word) 207 | { 208 | case "#if": 209 | { 210 | addToken(TokenType.S_IF); 211 | break; 212 | } 213 | case "#ifdef": 214 | { 215 | addToken(TokenType.S_IFDEF); 216 | break; 217 | } 218 | case "#ifndef": 219 | { 220 | addToken(TokenType.S_IFNDEF); 221 | break; 222 | } 223 | case "#endif": 224 | { 225 | addToken(TokenType.S_ENDIF); 226 | break; 227 | } 228 | case "#define": 229 | { 230 | addToken(TokenType.S_DEFINE); 231 | break; 232 | } 233 | case "then": 234 | { 235 | addToken(TokenType.THEN); 236 | break; 237 | } 238 | case "typedef": 239 | { 240 | addToken(TokenType.TYPEDEF); 241 | break; 242 | } 243 | case "using": 244 | { 245 | addToken(TokenType.USING); 246 | break; 247 | } 248 | case "const": 249 | { 250 | addToken(TokenType.CONSTANT); 251 | break; 252 | } 253 | case "inst": 254 | { 255 | addToken(TokenType.INST_ENUM); 256 | break; 257 | } 258 | case "ldef": 259 | { 260 | addToken(TokenType.LDEF); 261 | break; 262 | } 263 | case "switch": 264 | { 265 | addToken(TokenType.SWITCH); 266 | break; 267 | } 268 | case "case": 269 | { 270 | addToken(TokenType.CASE); 271 | break; 272 | } 273 | case "statement": 274 | { 275 | addToken(TokenType.STATEMENT); 276 | break; 277 | } 278 | case "lambda": 279 | { 280 | addToken(TokenType.LAMBDA); 281 | break; 282 | } 283 | case "catch": 284 | { 285 | addToken(TokenType.CATCH); 286 | break; 287 | } 288 | case "await": 289 | { 290 | addToken(TokenType.AWAIT); 291 | break; 292 | } 293 | case "sout": 294 | { 295 | addToken(TokenType.PRINT); 296 | break; 297 | } 298 | case "var": 299 | { 300 | addToken(TokenType.OBJ); 301 | break; 302 | } 303 | case "enum": 304 | { 305 | addToken(TokenType.ENUM); 306 | break; 307 | } 308 | case "if": 309 | { 310 | addToken(TokenType.IF); 311 | break; 312 | } 313 | case "else": 314 | { 315 | addToken(TokenType.ELSE); 316 | break; 317 | } 318 | 319 | case "try": 320 | { 321 | addToken(TokenType.TRY); 322 | break; 323 | } 324 | 325 | case "while": 326 | { 327 | addToken(TokenType.WHILE); 328 | break; 329 | } 330 | case "for": 331 | { 332 | addToken(TokenType.FOR); 333 | break; 334 | } 335 | case "foreach": 336 | { 337 | addToken(TokenType.FOREACH); 338 | break; 339 | } 340 | case "to": 341 | { 342 | addToken(TokenType.TO); 343 | break; 344 | } 345 | 346 | case "do": 347 | { 348 | addToken(TokenType.DO); 349 | break; 350 | } 351 | case "break": 352 | { 353 | addToken(TokenType.BREAK); 354 | break; 355 | } 356 | case "continue": 357 | { 358 | addToken(TokenType.CONTINUE); 359 | break; 360 | } 361 | case "def": 362 | { 363 | addToken(TokenType.DEF); 364 | break; 365 | } 366 | case "class": 367 | { 368 | addToken(TokenType.CLASS); 369 | break; 370 | } 371 | case "init": 372 | { 373 | addToken(TokenType.RUN_CLASS); 374 | break; 375 | } 376 | case "return": 377 | { 378 | addToken(TokenType.RETURN); 379 | break; 380 | } 381 | case "use": 382 | { 383 | addToken(TokenType.USE); 384 | break; 385 | } 386 | case "true": 387 | { 388 | addToken(TokenType.TRUE); 389 | break; 390 | } 391 | case "false": 392 | { 393 | addToken(TokenType.FALSE); 394 | break; 395 | } 396 | default: 397 | { 398 | addToken(TokenType.WORD, word); 399 | break; 400 | } 401 | } 402 | } 403 | 404 | private void tokenizeOperator() 405 | { 406 | char current = peek(0); 407 | if (current == '/') 408 | { 409 | if (peek(1) == '/') 410 | { 411 | next(); 412 | next(); 413 | tokenizeComment(); 414 | return; 415 | } 416 | else if (peek(1) == '*') 417 | { 418 | next(); 419 | next(); 420 | tokenizeMultilineComment(); 421 | return; 422 | } 423 | } 424 | 425 | StringBuilder buffer = new StringBuilder(); 426 | while (true) 427 | { 428 | string text = buffer.ToString(); 429 | if (!OPERATORS.ContainsKey(text + current) && text != null) 430 | { 431 | addToken(OPERATORS[text]); 432 | return; 433 | } 434 | buffer.Append(current); 435 | current = next(); 436 | } 437 | } 438 | 439 | private void tokenizeMultilineComment() 440 | { 441 | char current = peek(0); 442 | while (true) 443 | { 444 | if (current == '\0') throw new Exception("Missing close tag"); 445 | if (current == '*' && peek(1) == '/') break; 446 | current = next(); 447 | } 448 | next(); 449 | next(); 450 | } 451 | 452 | private void tokenizeComment() 453 | { 454 | char current = peek(0); 455 | while ("\r\n\0".IndexOf(current) == -1) 456 | { 457 | current = next(); 458 | } 459 | } 460 | 461 | private void tokenizeNumber() 462 | { 463 | StringBuilder buffer = new StringBuilder(); 464 | char current = peek(0); 465 | while (true) 466 | { 467 | if (current == ',') 468 | { 469 | if (buffer.ToString().IndexOf(',') != -1) 470 | { 471 | throw new Exception("Invalid float number"); 472 | } 473 | } 474 | else if (!Char.IsDigit(current)) 475 | { 476 | break; 477 | } 478 | buffer.Append(current); 479 | current = next(); 480 | } 481 | addToken(TokenType.NUMBER, buffer.ToString()); 482 | } 483 | 484 | private char next() 485 | { 486 | pos++; 487 | return peek(0); 488 | } 489 | 490 | private char peek(int relativePosition) 491 | { 492 | int position = pos + relativePosition; 493 | if (position >= length) 494 | { 495 | return '\0'; 496 | } 497 | return input[position]; 498 | } 499 | 500 | private void addToken(TokenType type) 501 | { 502 | addToken(type, ""); 503 | } 504 | 505 | private void addToken(TokenType type, string text) 506 | { 507 | tokens.Add(new Token(type, text)); 508 | } 509 | } 510 | } -------------------------------------------------------------------------------- /SourceCode/Parser/Parser.cs: -------------------------------------------------------------------------------- 1 | using OwnLang.ast.lib; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace OwnLang.ast 9 | { 10 | public class Parser 11 | { 12 | private List tokens; 13 | private int pos = 0; 14 | private int size; 15 | private static Token EOF = new Token(TokenType.EOF, ""); 16 | 17 | public Parser(List tokens) 18 | { 19 | this.tokens = tokens; 20 | size = tokens.Count(); 21 | } 22 | 23 | public Statement parse() 24 | { 25 | BlockStatement result = new BlockStatement(); 26 | 27 | while (!match(TokenType.EOF)) 28 | { 29 | try 30 | { 31 | result.add(statement()); 32 | } 33 | catch(EndIfException) {} 34 | } 35 | return result; 36 | } 37 | 38 | private Statement block() 39 | { 40 | BlockStatement block = new BlockStatement(); 41 | consume(TokenType.LBRACE); 42 | while (!match(TokenType.RBRACE)) 43 | { 44 | block.add(statement()); 45 | } 46 | return block; 47 | } 48 | 49 | private Statement statementOrBlock() 50 | { 51 | if (get(0).getType() == TokenType.LBRACE) return block(); 52 | return statement(); 53 | } 54 | 55 | private Statement statement() 56 | { 57 | if (match(TokenType.FOREACH)) 58 | { 59 | return foreachStatement(); 60 | } 61 | if (match(TokenType.PRINT)) 62 | { 63 | return new PrintStatement(expression()); 64 | } 65 | if (match(TokenType.IF)) 66 | { 67 | return ifElse(); 68 | } 69 | if (match(TokenType.WHILE)) 70 | { 71 | return whileStatement(); 72 | } 73 | if (match(TokenType.TRY)) 74 | { 75 | return tryStatement(); 76 | } 77 | if (match(TokenType.DO)) 78 | { 79 | return doWhileStatement(); 80 | } 81 | if (match(TokenType.STATEMENT)) 82 | { 83 | return new StatementStatement(statementOrBlock()); 84 | } 85 | if (match(TokenType.BREAK)) 86 | { 87 | return new BreakStatement(); 88 | } 89 | if (match(TokenType.CONTINUE)) 90 | { 91 | return new ContinueStatement(); 92 | } 93 | if (match(TokenType.RETURN)) 94 | { 95 | return new ReturnStatement(expression()); 96 | } 97 | if (match(TokenType.SWITCH)) 98 | { 99 | return switchs(); 100 | } 101 | if (match(TokenType.USE)) 102 | { 103 | return new UseStatement(expression().ToString()); 104 | } 105 | if (match(TokenType.FOR)) 106 | { 107 | return forStatement(); 108 | } 109 | if (match(TokenType.DEF)) 110 | { 111 | return functionDefine(); 112 | } 113 | if (match(TokenType.CLASS)) 114 | { 115 | return classDefine(); 116 | } 117 | if(match(TokenType.ENUM)) 118 | { 119 | return enums(); 120 | } 121 | if (get(0).getType() == TokenType.AWAIT && get(1).getType() == TokenType.WORD && get(2).getType() == TokenType.LPAREN) 122 | { 123 | return new AsyncFunctionStatement(async_function()); 124 | } 125 | else if (get(0).getType() == TokenType.WORD && get(1).getType() == TokenType.LPAREN) 126 | { 127 | return new FunctionStatement(function()); 128 | } 129 | else if (get(0).getType() == TokenType.WORD && get(1).getType() == TokenType.DDOT && get(2).getType() == TokenType.WORD && get(3).getType() == TokenType.LPAREN) 130 | { 131 | return new FunctionStatement(function()); 132 | } 133 | if (get(0).getType() == TokenType.RUN_CLASS && get(1).getType() == TokenType.WORD && get(2).getType() == TokenType.LPAREN) 134 | { 135 | return new ClassStatement(classes()); 136 | } 137 | if (get(0).getType() == TokenType.S_DEFINE) 138 | { 139 | consume(TokenType.S_DEFINE); 140 | string name = consume(TokenType.WORD).getText(); 141 | Expression expr = expression(); 142 | Marks.marks[name] = expr; 143 | return new NanStatement(); 144 | } 145 | if (get(0).getType() == TokenType.S_IF) 146 | { 147 | consume(TokenType.S_IF); 148 | int expr = expression().eval().asNumber(); 149 | if (expr != 1) { 150 | for(; ; ) { 151 | try { 152 | if (get(0).getType() == TokenType.S_ENDIF) 153 | { 154 | consume(TokenType.S_ENDIF); 155 | throw new EndIfException(); 156 | } 157 | pos++; 158 | } 159 | catch (EndIfException) { 160 | break; 161 | } 162 | finally { 163 | 164 | } 165 | } 166 | } 167 | return new NanStatement(); 168 | } 169 | if (get(0).getType() == TokenType.S_IFDEF) 170 | { 171 | consume(TokenType.S_IFDEF); 172 | string expr = consume(TokenType.WORD).getText(); 173 | 174 | if (!Marks.marks.ContainsKey(expr)) { 175 | for (; ; ) { 176 | try { 177 | if (get(0).getType() == TokenType.S_ENDIF) 178 | { 179 | consume(TokenType.S_ENDIF); 180 | throw new EndIfException(); 181 | } 182 | pos++; 183 | } 184 | catch (EndIfException) { 185 | break; 186 | } 187 | finally { 188 | 189 | } 190 | } 191 | } 192 | return new NanStatement(); 193 | } 194 | if (get(0).getType() == TokenType.S_IFNDEF) 195 | { 196 | consume(TokenType.S_IFNDEF); 197 | string expr = consume(TokenType.WORD).getText(); 198 | 199 | if (Marks.marks.ContainsKey(expr)) { 200 | for (; ; ) { 201 | try { 202 | if (get(0).getType() == TokenType.S_ENDIF) 203 | { 204 | consume(TokenType.S_ENDIF); 205 | throw new EndIfException(); 206 | } 207 | pos++; 208 | } 209 | catch (EndIfException){ 210 | break; 211 | } 212 | finally { 213 | 214 | } 215 | } 216 | } 217 | return new NanStatement(); 218 | } 219 | if (get(0).getType() == TokenType.S_ENDIF) 220 | { 221 | consume(TokenType.S_ENDIF); 222 | throw new EndIfException(); 223 | } 224 | return assignmentStatement(); 225 | } 226 | 227 | private ClassDefine classDefine() 228 | { 229 | string name = consume(TokenType.WORD).getText(); 230 | consume(TokenType.LPAREN); 231 | List argNames = new List(); 232 | while (!match(TokenType.RPAREN)) 233 | { 234 | argNames.Add(consume(TokenType.WORD).getText()); 235 | match(TokenType.COMMA); 236 | } 237 | Statement body = statementOrBlock(); 238 | return new ClassDefine(name, body); 239 | } 240 | 241 | private FunctionDefine functionDefine() 242 | { 243 | string name = consume(TokenType.WORD).getText(); 244 | consume(TokenType.LPAREN); 245 | List argNames = new List(); 246 | while (!match(TokenType.RPAREN)) 247 | { 248 | argNames.Add(consume(TokenType.WORD).getText()); 249 | match(TokenType.COMMA); 250 | } 251 | Statement body = statementOrBlock(); 252 | return new FunctionDefine(name, argNames, body); 253 | } 254 | 255 | private Statement switchs() 256 | { 257 | Expression expr = expression(); 258 | List caseStatements = new List(); 259 | consume(TokenType.LBRACE); 260 | 261 | while (!(match(TokenType.RBRACE))) 262 | { 263 | consume(TokenType.CASE); 264 | Expression curr = expression(); 265 | caseStatements.Add(new CaseStatement(curr, expr, block())); 266 | } 267 | 268 | return new SwitchStatement(caseStatements); 269 | } 270 | 271 | private Statement enums() 272 | { 273 | string name = consume(TokenType.WORD).getText(); 274 | Dictionary enums = new Dictionary(); 275 | 276 | consume(TokenType.LBRACE); 277 | while(!(match(TokenType.RBRACE))) 278 | { 279 | string en = consume(TokenType.WORD).getText(); 280 | match(TokenType.COMMA); 281 | enums.Add(en, new StringValue(en)); 282 | } 283 | //Variables.set(name, new EnumValue(enums)); 284 | //return new NanStatement(); 285 | return new AssignmentStatement(name, new ValueExpression(new EnumValue(enums))); 286 | } 287 | 288 | private Statement assignmentStatement() 289 | { 290 | Token current = get(0); 291 | if (current.getType() == TokenType.WORD && get(1).getType() == TokenType.EQ) 292 | { 293 | string variable = consume(TokenType.WORD).getText(); 294 | consume(TokenType.EQ); 295 | return new AssignmentStatement(variable, expression()); 296 | } 297 | 298 | if (current.getType() == TokenType.TYPEDEF && get(1).getType() == TokenType.CONSTANT) 299 | { 300 | consume(TokenType.TYPEDEF); 301 | consume(TokenType.CONSTANT); 302 | string type = consume(TokenType.WORD).getText(); 303 | string variable = consume(TokenType.WORD).getText(); 304 | consume(TokenType.EQ); 305 | AssignmentStatement assignmentStatement = new AssignmentStatement(variable, expression(), true, true); 306 | switch (type) 307 | { 308 | case "int": 309 | { 310 | assignmentStatement.value = new NumberValue(0); 311 | break; 312 | } 313 | case "enum": 314 | { 315 | assignmentStatement.value = new EnumValue(new Dictionary()); 316 | break; 317 | } 318 | case "string": 319 | { 320 | assignmentStatement.value = new StringValue(null); 321 | break; 322 | } 323 | case "dict": 324 | { 325 | assignmentStatement.value = new DictionaryValue(0); 326 | break; 327 | } 328 | case "stack": 329 | { 330 | assignmentStatement.value = new StackValue(0); 331 | break; 332 | } 333 | case "bool": 334 | { 335 | assignmentStatement.value = new BoolValue(true); 336 | break; 337 | } 338 | case "obj": 339 | { 340 | assignmentStatement.value = new ObjectValue(null); 341 | break; 342 | } 343 | case "arr": 344 | { 345 | assignmentStatement.value = new ArrayValue(0); 346 | break; 347 | } 348 | } 349 | return assignmentStatement; 350 | } 351 | 352 | if (current.getType() == TokenType.USING) 353 | { 354 | consume(TokenType.USING); 355 | consume(TokenType.LPAREN); 356 | string name = consume(TokenType.WORD).getText(); 357 | consume(TokenType.EQ); 358 | Expression expr = expression(); 359 | consume(TokenType.RPAREN); 360 | Statement stat = statementOrBlock(); 361 | return new UsingStatement(stat, name, expr); 362 | } 363 | 364 | if (current.getType() == TokenType.CONSTANT) 365 | { 366 | consume(TokenType.CONSTANT); 367 | string variable = consume(TokenType.WORD).getText(); 368 | consume(TokenType.EQ); 369 | return new AssignmentStatement(variable, expression(), false, true); 370 | } 371 | 372 | if (current.getType() == TokenType.TYPEDEF) 373 | { 374 | consume(TokenType.TYPEDEF); 375 | string type = consume(TokenType.WORD).getText(); 376 | string variable = consume(TokenType.WORD).getText(); 377 | consume(TokenType.EQ); 378 | AssignmentStatement assignmentStatement = new AssignmentStatement(variable, expression(), true); 379 | switch (type) 380 | { 381 | case "int": 382 | { 383 | assignmentStatement.value = new NumberValue(0); 384 | break; 385 | } 386 | case "enum": 387 | { 388 | assignmentStatement.value = new EnumValue(new Dictionary()); 389 | break; 390 | } 391 | case "string": 392 | { 393 | assignmentStatement.value = new StringValue(null); 394 | break; 395 | } 396 | case "dict": 397 | { 398 | assignmentStatement.value = new DictionaryValue(0); 399 | break; 400 | } 401 | case "stack": 402 | { 403 | assignmentStatement.value = new StackValue(0); 404 | break; 405 | } 406 | case "bool": 407 | { 408 | assignmentStatement.value = new BoolValue(true); 409 | break; 410 | } 411 | case "obj": 412 | { 413 | assignmentStatement.value = new ObjectValue(null); 414 | break; 415 | } 416 | case "arr": 417 | { 418 | assignmentStatement.value = new ArrayValue(0); 419 | break; 420 | } 421 | } 422 | return assignmentStatement; 423 | } 424 | 425 | if (current.getType() == TokenType.WORD && get(1).getType() == TokenType.WORD && get(2).getType() == TokenType.DDOTEQ) 426 | { 427 | string enums = consume(TokenType.WORD).getText(); 428 | string variable = consume(TokenType.WORD).getText(); 429 | consume(TokenType.DDOTEQ); 430 | DdotEqAssignmentStatement ddotEq = new DdotEqAssignmentStatement(enums, new StringValue(variable), expression()); 431 | return ddotEq; 432 | } 433 | if (current.getType() == TokenType.WORD && get(1).getType() == TokenType.PLUSEQ) 434 | { 435 | string variable = consume(TokenType.WORD).getText(); 436 | consume(TokenType.PLUSEQ); 437 | return new NewAssignmentStatement(variable, expression(), "+"); 438 | } 439 | 440 | if (current.getType() == TokenType.WORD && get(1).getType() == TokenType.INST_ENUM) 441 | { 442 | List fdelete = new List(); 443 | string enums = consume(TokenType.WORD).getText(); 444 | consume(TokenType.INST_ENUM); 445 | consume(TokenType.LPAREN); 446 | while (!match(TokenType.RPAREN)) 447 | { 448 | fdelete.Add(consume(TokenType.WORD).getText()); 449 | } 450 | string enumf = consume(TokenType.WORD).getText(); 451 | return new InstStatement(enums, enumf, fdelete); 452 | } 453 | 454 | if (current.getType() == TokenType.WORD && get(1).getType() == TokenType.MINUSEQ) 455 | { 456 | string variable = consume(TokenType.WORD).getText(); 457 | consume(TokenType.MINUSEQ); 458 | return new NewAssignmentStatement(variable, expression(), "-"); 459 | } 460 | 461 | if (current.getType() == TokenType.WORD && get(1).getType() == TokenType.STAREQ) 462 | { 463 | string variable = consume(TokenType.WORD).getText(); 464 | consume(TokenType.STAREQ); 465 | return new NewAssignmentStatement(variable, expression(), "*"); 466 | } 467 | 468 | if (lookMatch(0, TokenType.STARCON)) 469 | { 470 | return starcon(); 471 | } 472 | 473 | if (current.getType() == TokenType.WORD && get(1).getType() == TokenType.SLASHEQ) 474 | { 475 | string variable = consume(TokenType.WORD).getText(); 476 | consume(TokenType.SLASHEQ); 477 | return new NewAssignmentStatement(variable, expression(), "/"); 478 | } 479 | 480 | if (current.getType() == TokenType.WORD && get(1).getType() == TokenType.INC) 481 | { 482 | string variable = consume(TokenType.WORD).getText(); 483 | consume(TokenType.INC); 484 | return new SuffixAssignmentStatement(variable, "+"); 485 | } 486 | 487 | if (current.getType() == TokenType.WORD && get(1).getType() == TokenType.DEC) 488 | { 489 | string variable = consume(TokenType.WORD).getText(); 490 | consume(TokenType.DEC); 491 | return new SuffixAssignmentStatement(variable, "-"); 492 | } 493 | 494 | if (current.getType() == TokenType.WORD && get(1).getType() == TokenType.LBRAСKET) 495 | { 496 | string variable = consume(TokenType.WORD).getText(); 497 | consume(TokenType.LBRAСKET); 498 | Expression index = expression(); 499 | consume(TokenType.RBRACKET); 500 | consume(TokenType.EQ); 501 | return new ArrayAssignmentStatement(variable, index, expression()); 502 | } 503 | if (current.getType() == TokenType.OBJ && get(1).getType() == TokenType.WORD) 504 | { 505 | string obj = consume(TokenType.OBJ).getText(); 506 | string variable = consume(TokenType.WORD).getText(); 507 | //Variables.set(variable, new NumberValue(0)); 508 | return new AssignmentStatement(variable, new ValueExpression(0)); 509 | } 510 | throw new Exception("Unknow statement"); 511 | } 512 | 513 | private Statement ifElse() 514 | { 515 | Expression condition = expression(); 516 | Statement ifStatement = statementOrBlock(); 517 | Statement elseStatement; 518 | 519 | if (match(TokenType.ELSE)) 520 | { 521 | elseStatement = statementOrBlock(); 522 | } 523 | else 524 | { 525 | elseStatement = null; 526 | } 527 | 528 | return new IfStatement(condition, ifStatement, elseStatement); 529 | } 530 | 531 | private Statement whileStatement() 532 | { 533 | //consume(TokenType.WHILE); 534 | Expression condition = expression(); 535 | Statement statement = statementOrBlock(); 536 | return new WhileStatement(condition, statement); 537 | } 538 | 539 | private Statement tryStatement() 540 | { 541 | Statement statement_try = statementOrBlock(); 542 | Statement statement_catch; 543 | if (match(TokenType.CATCH)) 544 | { 545 | statement_catch = statementOrBlock(); 546 | } 547 | else 548 | { 549 | throw new Exception("Catch block wasn\'t found"); 550 | } 551 | return new TryCatchStatement(statement_try, statement_catch); 552 | } 553 | 554 | private Statement doWhileStatement() 555 | { 556 | //consume(TokenType.WHILE); 557 | Statement statement = statementOrBlock(); 558 | consume(TokenType.WHILE); 559 | Expression condition = expression(); 560 | return new DoWhileStatement(statement, condition); 561 | } 562 | 563 | private Statement foreachStatement() 564 | { 565 | Expression initalization = expression(); 566 | consume(TokenType.TO); 567 | Expression termination = expression(); 568 | Statement statement = statementOrBlock(); 569 | return new ForeachStatement(termination.ToString(), initalization, statement); 570 | } 571 | 572 | private Statement forStatement() 573 | { 574 | Statement initalization = assignmentStatement(); 575 | consume(TokenType.COMMA); 576 | Expression termination = expression(); 577 | consume(TokenType.COMMA); 578 | Statement increment = assignmentStatement(); 579 | Statement statement = statementOrBlock(); 580 | return new ForStatement(termination, statement, initalization, increment); 581 | } 582 | 583 | private Expression expression() 584 | { 585 | return logicalOr(); 586 | } 587 | 588 | private Expression logicalOr() 589 | { 590 | Expression result = logicalAnd(); 591 | 592 | while (true) 593 | { 594 | if (match(TokenType.BARBAR)) 595 | { 596 | result = new ConditionalExpression(ConditionalExpression.Operator.OR, result, logicalAnd()); 597 | continue; 598 | } 599 | if (match(TokenType.BAR)) 600 | { 601 | result = new ConditionalExpression(ConditionalExpression.Operator.LOGOR, result, logicalAnd()); 602 | continue; 603 | } 604 | if (match(TokenType.XOR)) 605 | { 606 | result = new ConditionalExpression(ConditionalExpression.Operator.XOR, result, logicalAnd()); 607 | continue; 608 | } 609 | break; 610 | } 611 | 612 | return result; 613 | } 614 | 615 | private Expression logicalAnd() 616 | { 617 | Expression result = equality(); 618 | 619 | while (true) 620 | { 621 | if (match(TokenType.AMPAMP)) 622 | { 623 | result = new ConditionalExpression(ConditionalExpression.Operator.AND, result, equality()); 624 | continue; 625 | } 626 | if (match(TokenType.AMP)) 627 | { 628 | result = new ConditionalExpression(ConditionalExpression.Operator.AND, result, equality()); 629 | continue; 630 | } 631 | break; 632 | } 633 | 634 | return result; 635 | } 636 | 637 | private Expression equality() 638 | { 639 | Expression result = conditional(); 640 | 641 | if (match(TokenType.EXCLEQ)) 642 | { 643 | return new ConditionalExpression(ConditionalExpression.Operator.NOT_EQUALS, result, conditional()); 644 | } 645 | 646 | 647 | if (match(TokenType.EQEQ)) 648 | { 649 | return new ConditionalExpression(ConditionalExpression.Operator.EQUALS, result, conditional()); 650 | } 651 | 652 | return result; 653 | } 654 | 655 | private Expression conditional() 656 | { 657 | Expression result = suffix(); 658 | 659 | while (true) 660 | { 661 | if (match(TokenType.EXCLEQ)) 662 | { 663 | result = new ConditionalExpression(ConditionalExpression.Operator.NOT_EQUALS, result, suffix()); 664 | continue; 665 | } 666 | 667 | 668 | if (match(TokenType.EQEQ)) 669 | { 670 | result = new ConditionalExpression(ConditionalExpression.Operator.EQUALS, result, suffix()); 671 | continue; 672 | } 673 | 674 | if (match(TokenType.LT)) 675 | { 676 | result = new ConditionalExpression(ConditionalExpression.Operator.LT, result, suffix()); 677 | continue; 678 | } 679 | 680 | if (match(TokenType.LTEQ)) 681 | { 682 | result = new ConditionalExpression(ConditionalExpression.Operator.LTEQ, result, suffix()); 683 | continue; 684 | } 685 | 686 | if (match(TokenType.GT)) 687 | { 688 | result = new ConditionalExpression(ConditionalExpression.Operator.GT, result, suffix()); 689 | continue; 690 | } 691 | 692 | if (match(TokenType.GTEQ)) 693 | { 694 | result = new ConditionalExpression(ConditionalExpression.Operator.GTEQ, result, suffix()); 695 | continue; 696 | } 697 | break; 698 | } 699 | 700 | return result; 701 | } 702 | 703 | private Expression suffix() 704 | { 705 | Expression result = foreq(); 706 | 707 | while (true) 708 | { 709 | if (match(TokenType.INC)) 710 | { 711 | result = new SuffixExpression('+', result); 712 | continue; 713 | } 714 | 715 | if (match(TokenType.DEC)) 716 | { 717 | result = new SuffixExpression('-', result); 718 | continue; 719 | } 720 | 721 | if (match(TokenType.LOGNOT)) 722 | { 723 | result = new SuffixExpression('~', result); 724 | continue; 725 | } 726 | 727 | break; 728 | } 729 | 730 | return result; 731 | } 732 | 733 | private Expression foreq() 734 | { 735 | Expression result = additive(); 736 | 737 | while (true) 738 | { 739 | if (match(TokenType.PLUSEQ)) 740 | { 741 | result = new ForEqExpression('+', result, additive()); 742 | continue; 743 | } 744 | 745 | if (match(TokenType.MINUSEQ)) 746 | { 747 | result = new ForEqExpression('-', result, additive()); 748 | continue; 749 | } 750 | 751 | if (match(TokenType.STAREQ)) 752 | { 753 | result = new ForEqExpression('*', result, additive()); 754 | continue; 755 | } 756 | 757 | if (match(TokenType.SLASHEQ)) 758 | { 759 | result = new ForEqExpression('/', result, additive()); 760 | continue; 761 | } 762 | 763 | break; 764 | } 765 | 766 | return result; 767 | } 768 | 769 | private Expression additive() 770 | { 771 | Expression result = multiplicative(); 772 | 773 | while (true) 774 | { 775 | if (match(TokenType.PLUS)) 776 | { 777 | result = new BinaryExpression('+', result, multiplicative()); 778 | continue; 779 | } 780 | 781 | if (match(TokenType.MINUS)) 782 | { 783 | result = new BinaryExpression('-', result, multiplicative()); 784 | continue; 785 | } 786 | 787 | break; 788 | } 789 | 790 | return result; 791 | } 792 | 793 | private Expression multiplicative() 794 | { 795 | Expression result = unary(); 796 | 797 | while (true) 798 | { 799 | if (match(TokenType.STAR)) 800 | { 801 | result = new BinaryExpression('*', result, unary()); 802 | continue; 803 | } 804 | 805 | if (match(TokenType.SLASH)) 806 | { 807 | result = new BinaryExpression('/', result, unary()); 808 | continue; 809 | } 810 | 811 | if (match(TokenType.STARSTAR)) 812 | { 813 | result = new BinaryExpression('*', result, unary()).set_op1("**"); 814 | continue; 815 | } 816 | 817 | if (match(TokenType.PERCENT)) 818 | { 819 | result = new BinaryExpression('%', result, unary()); 820 | } 821 | break; 822 | } 823 | return result; 824 | } 825 | 826 | private Expression unary() 827 | { 828 | if (match(TokenType.MINUS)) 829 | { 830 | return new UnaryExpression('-', primary()); 831 | } 832 | if (match(TokenType.PLUS)) 833 | { 834 | return new UnaryExpression('+', primary()); 835 | } 836 | return primary(); 837 | } 838 | 839 | private Expression primary() 840 | { 841 | Token current = get(0); 842 | if (match(TokenType.NUMBER)) 843 | { 844 | return new ValueExpression(double.Parse(current.getText())); 845 | } 846 | if (match(TokenType.TRUE)) 847 | { 848 | return new ValueExpression(true); 849 | } 850 | if (match(TokenType.FALSE)) 851 | { 852 | return new ValueExpression(false); 853 | } 854 | if(lookMatch(0, TokenType.LDEF)) 855 | { 856 | return ldef(); 857 | } 858 | if(lookMatch(0, TokenType.WORD) && lookMatch(1, TokenType.DDOT)) 859 | { 860 | return ddot(); 861 | } 862 | if (lookMatch(0, TokenType.LAMBDA)) 863 | { 864 | return lambda(); 865 | } 866 | if (get(0).getType() == TokenType.WORD && get(1).getType() == TokenType.DDOT && get(2).getType() == TokenType.WORD && get(3).getType() == TokenType.LPAREN) 867 | { 868 | return function(); 869 | } 870 | if (lookMatch(0, TokenType.WORD) && lookMatch(1, TokenType.LPAREN)) 871 | { 872 | return function(); 873 | } 874 | if (lookMatch(0, TokenType.WORD) && lookMatch(1, TokenType.LBRAСKET)) 875 | { 876 | return element(); 877 | } 878 | if (lookMatch(0, TokenType.STARCON)) 879 | { 880 | return starcon(); 881 | } 882 | if (lookMatch(0, TokenType.WORD) && lookMatch(1, TokenType.LBRACE)) 883 | { 884 | return element(); 885 | } 886 | if (lookMatch(0, TokenType.DOG)) 887 | { 888 | return dog(); 889 | } 890 | if (lookMatch(0, TokenType.WORD) && lookMatch(1, TokenType.EQCON)) 891 | { 892 | return eqcon(); 893 | } 894 | if (lookMatch(0, TokenType.IF)) 895 | { 896 | return ifThenElse(); 897 | } 898 | if (match(TokenType.WORD) && Marks.marks.ContainsKey(current.getText())) 899 | { 900 | if (Marks.marks[current.getText()] != null) 901 | return Marks.marks[current.getText()]; 902 | } 903 | if (match(TokenType.WORD)) 904 | { 905 | return new ConstantExpression(current.getText()); 906 | } 907 | if (match(TokenType.TEXT)) 908 | { 909 | return new ValueExpression(current.getText()); 910 | } 911 | if (match(TokenType.LPAREN)) 912 | { 913 | Expression result = expression(); 914 | match(TokenType.RPAREN); 915 | return result; 916 | } 917 | throw new Exception("unknown expression"); 918 | } 919 | 920 | private IfThenElseExpression ifThenElse() 921 | { 922 | consume(TokenType.IF); 923 | Expression condition = expression(); 924 | consume(TokenType.THEN); 925 | Expression ifStatement = expression(); 926 | Expression elseStatement; 927 | 928 | if (match(TokenType.ELSE)) 929 | { 930 | elseStatement = expression(); 931 | } 932 | else 933 | { 934 | elseStatement = null; 935 | } 936 | 937 | return new IfThenElseExpression(condition, ifStatement, elseStatement); 938 | } 939 | 940 | private Starcon starcon() 941 | { 942 | consume(TokenType.STARCON); 943 | Expression iter = expression(); 944 | Expression expr = expression(); 945 | return new Starcon(iter, expr); 946 | } 947 | 948 | private EqconExpression eqcon() 949 | { 950 | string var = consume(TokenType.WORD).getText(); 951 | consume(TokenType.EQCON); 952 | Expression value = expression(); 953 | return new EqconExpression(var, value); 954 | } 955 | 956 | private DogExpression dog() 957 | { 958 | consume(TokenType.DOG); 959 | consume(TokenType.LDEF); 960 | List argNames = new List(); 961 | Statement body = statementOrBlock(); 962 | return new DogExpression(new LdefExpression(body, argNames)); 963 | } 964 | 965 | private Expression element() 966 | { 967 | string variable = consume(TokenType.WORD).getText(); 968 | try 969 | { 970 | consume(TokenType.LBRAСKET); 971 | Expression index = expression(); 972 | consume(TokenType.RBRACKET); 973 | return new ArrayAccesExpression(variable, index); 974 | } 975 | catch 976 | { 977 | consume(TokenType.LBRACE); 978 | Expression index = expression(); 979 | consume(TokenType.RBRACE); 980 | return new StackAccesExpression(variable, index); 981 | } 982 | } 983 | 984 | private LambdaExpression lambda() 985 | { 986 | consume(TokenType.LAMBDA); 987 | LambdaExpression lambda = new LambdaExpression(statement()); 988 | return lambda; 989 | } 990 | 991 | private DdotExpression ddot() 992 | { 993 | //new ValueExpression(); 994 | 995 | string value = consume(TokenType.WORD).getText(); 996 | consume(TokenType.DDOT); 997 | //string name = consume(TokenType.WORD).getText(); 998 | //StringValue name = new StringValue(consume(TokenType.WORD).getText());string value = consume(TokenType.WORD).getText(); 999 | string name = consume(TokenType.WORD).getText(); 1000 | 1001 | DdotExpression ddot = new DdotExpression(value, name); 1002 | return ddot; 1003 | } 1004 | 1005 | private LdefExpression ldef() 1006 | { 1007 | consume(TokenType.LDEF); 1008 | consume(TokenType.LPAREN); 1009 | List argNames = new List(); 1010 | while (!match(TokenType.RPAREN)) 1011 | { 1012 | argNames.Add(consume(TokenType.WORD).getText()); 1013 | match(TokenType.COMMA); 1014 | } 1015 | Statement body = statementOrBlock(); 1016 | return new LdefExpression(body, argNames); 1017 | } 1018 | 1019 | private FunctionalExpression function() 1020 | { 1021 | string name = consume(TokenType.WORD).getText(); 1022 | if (lookMatch(0, TokenType.DDOT)) 1023 | { 1024 | consume(TokenType.DDOT); 1025 | string valname = consume(TokenType.WORD).getText(); 1026 | consume(TokenType.LPAREN); 1027 | FunctionalExpression efunction = new FunctionalExpression(valname); 1028 | efunction.enumFunc = name; 1029 | while (!match(TokenType.RPAREN)) 1030 | { 1031 | efunction.addArgument(expression()); 1032 | match(TokenType.COMMA); 1033 | } 1034 | return efunction; 1035 | } 1036 | 1037 | consume(TokenType.LPAREN); 1038 | FunctionalExpression function = new FunctionalExpression(name); 1039 | while (!match(TokenType.RPAREN)) 1040 | { 1041 | function.addArgument(expression()); 1042 | match(TokenType.COMMA); 1043 | } 1044 | return function; 1045 | } 1046 | 1047 | private FunctionalExpression async_function() 1048 | { 1049 | consume(TokenType.AWAIT); 1050 | string name = consume(TokenType.WORD).getText(); 1051 | consume(TokenType.LPAREN); 1052 | FunctionalExpression function = new FunctionalExpression(name); 1053 | while (!match(TokenType.RPAREN)) 1054 | { 1055 | function.addArgument(expression()); 1056 | match(TokenType.COMMA); 1057 | } 1058 | return function; 1059 | } 1060 | 1061 | private ClassExpression classes() 1062 | { 1063 | consume(TokenType.RUN_CLASS); 1064 | string name = consume(TokenType.WORD).getText(); 1065 | consume(TokenType.LPAREN); 1066 | ClassExpression classes = new ClassExpression(name); 1067 | while (!match(TokenType.RPAREN)) 1068 | { 1069 | //classes.addArgument(expression()); 1070 | match(TokenType.COMMA); 1071 | } 1072 | return classes; 1073 | } 1074 | 1075 | private Token consume(TokenType type) 1076 | { 1077 | Token current = get(0); 1078 | 1079 | if (type != current.getType()) throw new Exception("Token" + current + "doesn't match" + type); 1080 | pos++; 1081 | return current; 1082 | } 1083 | 1084 | private bool match(TokenType type) 1085 | { 1086 | Token current = get(0); 1087 | 1088 | if (type != current.getType()) return false; 1089 | pos++; 1090 | return true; 1091 | } 1092 | 1093 | private bool lookMatch(int index, TokenType type) 1094 | { 1095 | Token current = get(index); 1096 | 1097 | if (type != current.getType()) return false; 1098 | //pos++; 1099 | return true; 1100 | } 1101 | 1102 | public void gotoPos(int npos) => pos = npos; 1103 | 1104 | private Token get(int relativePosition) 1105 | { 1106 | int position = pos + relativePosition; 1107 | if (position >= size) 1108 | { 1109 | return EOF; 1110 | } 1111 | return tokens[position]; 1112 | } 1113 | } 1114 | } 1115 | -------------------------------------------------------------------------------- /SourceCode/Parser/Token.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang 8 | { 9 | public class Token 10 | { 11 | private TokenType type; 12 | 13 | private string text; 14 | 15 | public Token(TokenType type, string text) 16 | { 17 | this.type = type; 18 | this.text = text; 19 | } 20 | 21 | public TokenType getType() 22 | { 23 | return type; 24 | } 25 | 26 | public void setType(TokenType type) 27 | { 28 | this.type = type; 29 | } 30 | 31 | public string getText() 32 | { 33 | return text; 34 | } 35 | 36 | public void setText(string text) 37 | { 38 | this.text = text; 39 | } 40 | 41 | override public string ToString() 42 | { 43 | return type + " " + text; 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /SourceCode/Parser/TokenType.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang 8 | { 9 | public enum TokenType 10 | { 11 | NUMBER, 12 | WORD, 13 | TEXT, 14 | OBJ, 15 | TRUE, 16 | FALSE, 17 | ARRAY, 18 | DICT, 19 | STACK, 20 | OBJECT, 21 | BYTE, 22 | 23 | S_IF, 24 | S_ELSE, 25 | S_IFDEF, 26 | S_IFNDEF, 27 | S_ENDIF, 28 | S_DEFINE, 29 | THEN, 30 | USING, 31 | CONSTANT, 32 | TYPEDEF, 33 | PRINT, 34 | IF, 35 | TO, 36 | ELSE, 37 | WHILE, 38 | TRY, 39 | FOR, 40 | FOREACH, 41 | DO, 42 | BREAK, 43 | CONTINUE, 44 | DEF, 45 | LDEF, 46 | CLASS, 47 | RUN_CLASS, 48 | INST_ENUM, 49 | RETURN, 50 | USE, 51 | ENUM, 52 | ASYNC, 53 | AWAIT, 54 | CATCH, 55 | LAMBDA, 56 | STATEMENT, 57 | SWITCH, 58 | CASE, 59 | 60 | STAREQ, 61 | SLASHEQ, 62 | PLUSEQ, 63 | MINUSEQ, 64 | 65 | DOT, 66 | DDOT, 67 | DDOTEQ, 68 | STAR, 69 | SLASH, 70 | BIN, 71 | XOR, 72 | LOGNOT, 73 | PERCENT, 74 | STARSTAR, 75 | PLUS, 76 | MINUS, 77 | INC, 78 | DEC, 79 | EXCL, 80 | EXCLEQ, 81 | EQ, 82 | EQCON, 83 | STARCON, 84 | EQEQ, 85 | LT, 86 | LTEQ, 87 | GTEQ, 88 | GT, 89 | DOG, 90 | 91 | BAR, 92 | BARBAR, 93 | AMP, 94 | AMPAMP, 95 | 96 | LPAREN, 97 | RPAREN, 98 | LBRAСKET, 99 | RBRACKET, 100 | LBRACE, 101 | RBRACE, 102 | COMMA, 103 | 104 | EOF, 105 | 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /SourceCode/ast/ArrayAccesExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public class ArrayAccesExpression : Expression 10 | { 11 | private string variable; 12 | private Expression index; 13 | 14 | public ArrayAccesExpression(string variable, Expression index) 15 | { 16 | this.variable = variable; 17 | this.index = index; 18 | } 19 | 20 | public Value eval() 21 | { 22 | Value var = Variables.get(variable); 23 | //if (var is ArrayValue) 24 | if (var is DictionaryValue) 25 | { 26 | DictionaryValue array = (DictionaryValue)var; 27 | return array.get_by_index(index.eval().asString()); 28 | } 29 | try 30 | { 31 | ArrayValue array = (ArrayValue)var; 32 | return array.get_by_index((int)index.eval().asNumber()); 33 | } 34 | catch 35 | { 36 | StackValue array = (StackValue)var; 37 | return array.get_by_index((int)index.eval().asNumber()); 38 | } 39 | throw new Exception("Array expected"); 40 | } 41 | 42 | public override string ToString() 43 | { 44 | return String.Format((variable + '[' + index + ']').ToString(), variable, index); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /SourceCode/ast/ArrayAssignmentStatement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public class ArrayAssignmentStatement : Statement 10 | { 11 | private string variable; 12 | private Expression index; 13 | private Expression expression; 14 | 15 | public ArrayAssignmentStatement(string variable, Expression index, Expression expression) 16 | { 17 | this.variable = variable; 18 | this.index = index; 19 | this.expression = expression; 20 | } 21 | 22 | public void execute() 23 | { 24 | Value var = Variables.get(variable); 25 | //if (var is ArrayValue) 26 | if (var is DictionaryValue) 27 | { 28 | DictionaryValue array = (DictionaryValue)var; 29 | string dindex = index.eval().asString(); 30 | array.append(dindex, expression.eval()); 31 | return; 32 | } 33 | try 34 | { 35 | ArrayValue array = (ArrayValue)var; 36 | array.set((int)index.eval().asNumber(), expression.eval()); 37 | return; 38 | } 39 | catch 40 | { 41 | StackValue array = (StackValue)var; 42 | array.set((int)index.eval().asNumber(), expression.eval()); 43 | return; 44 | } 45 | throw new Exception("Array expected"); 46 | } 47 | 48 | public override string ToString() 49 | { 50 | return String.Format((variable + '[' + index + ']' + '=' + expression).ToString(), variable, index, expression); 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /SourceCode/ast/AssignmentStatement.cs: -------------------------------------------------------------------------------- 1 | using OwnLang.ast.lib; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace OwnLang.ast 9 | { 10 | class AssignmentStatement : Statement 11 | { 12 | private string variable; 13 | private Expression expression; 14 | private bool isTypedef; 15 | private bool isConst; 16 | public Value value; 17 | 18 | public AssignmentStatement(string variable, Expression expression, bool isTypedef = false, bool isConst=false) 19 | { 20 | this.variable = variable; 21 | this.expression = expression; 22 | this.isTypedef = isTypedef; 23 | this.isConst = isConst; 24 | } 25 | 26 | public void execute() 27 | { 28 | if (Constants.isExists(variable)) 29 | { 30 | throw new Exception("This is constant"); 31 | } 32 | Value result; 33 | if (isTypedef) 34 | { 35 | if (value is NumberValue) 36 | { 37 | result = (NumberValue)expression.eval(); 38 | if (isConst) 39 | { 40 | Constants.set(variable, result); 41 | return; 42 | } 43 | Variables.set(variable, result); 44 | return; 45 | } 46 | if (value is BoolValue) 47 | { 48 | result = (BoolValue)expression.eval(); 49 | if (isConst) 50 | { 51 | Constants.set(variable, result); 52 | return; 53 | } 54 | Variables.set(variable, result); 55 | return; 56 | } 57 | if (value is StringValue) 58 | { 59 | result = (StringValue)expression.eval(); 60 | if (isConst) 61 | { 62 | Constants.set(variable, result); 63 | return; 64 | } 65 | Variables.set(variable, result); 66 | return; 67 | } 68 | if (value is EnumValue) 69 | { 70 | result = (EnumValue)expression.eval(); 71 | if (isConst) 72 | { 73 | Constants.set(variable, result); 74 | return; 75 | } 76 | Variables.set(variable, result); 77 | return; 78 | } 79 | if (value is ObjectValue) 80 | { 81 | result = (ObjectValue)expression.eval(); 82 | if (isConst) 83 | { 84 | Constants.set(variable, result); 85 | return; 86 | } 87 | Variables.set(variable, result); 88 | return; 89 | } 90 | if (value is ArrayValue) 91 | { 92 | result = (ArrayValue)expression.eval(); 93 | if (isConst) 94 | { 95 | Constants.set(variable, result); 96 | return; 97 | } 98 | Variables.set(variable, result); 99 | return; 100 | } 101 | if (value is StackValue) 102 | { 103 | result = (StackValue)expression.eval(); 104 | if (isConst) 105 | { 106 | Constants.set(variable, result); 107 | return; 108 | } 109 | Variables.set(variable, result); 110 | return; 111 | } 112 | if (value is DictionaryValue) 113 | { 114 | result = (DictionaryValue)expression.eval(); 115 | if (isConst) 116 | { 117 | Constants.set(variable, result); 118 | return; 119 | } 120 | Variables.set(variable, result); 121 | return; 122 | } 123 | 124 | } 125 | result = expression.eval(); 126 | if (isConst) 127 | { 128 | Constants.set(variable, result); 129 | return; 130 | } 131 | Variables.set(variable, result); 132 | } 133 | 134 | public override string ToString() 135 | { 136 | return string.Format((variable.ToString() + " = " + expression.ToString()), variable, expression); 137 | } 138 | } 139 | } 140 | -------------------------------------------------------------------------------- /SourceCode/ast/BinaryExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public class BinaryExpression : Expression 10 | { 11 | private Expression expr1, expr2; 12 | private char operation; 13 | private string operation1 = string.Empty; 14 | 15 | public BinaryExpression(char operation, Expression expr1, Expression expr2) 16 | { 17 | this.operation = operation; 18 | this.expr1 = expr1; 19 | this.expr2 = expr2; 20 | } 21 | 22 | public BinaryExpression set_op1(string op1) 23 | { 24 | operation1 = op1; 25 | return this; 26 | } 27 | 28 | public Value eval() 29 | { 30 | Value value1 = expr1.eval(); 31 | Value value2 = expr2.eval(); 32 | if ((value1 is StringValue) || (value1 is ArrayValue)) 33 | { 34 | string string1 = value1.asString(); 35 | string string2 = value2.asString(); 36 | switch (operation) 37 | { 38 | case '+': 39 | { 40 | return new StringValue(string1 + string2); 41 | } 42 | case '*': 43 | { 44 | int iterations = (int)value2.asDouble(); 45 | StringBuilder buffer = new StringBuilder(); 46 | for (int i = 0; i < iterations; i++) 47 | { 48 | buffer.Append(string1); 49 | } 50 | return new StringValue(buffer.ToString()); 51 | } 52 | default: 53 | { 54 | return new StringValue(string1 + string2); 55 | } 56 | } 57 | } 58 | 59 | double number1 = value1.asDouble(); 60 | double number2 = value2.asDouble(); 61 | 62 | if (operation1 != string.Empty) 63 | { 64 | switch (operation1) 65 | { 66 | case "**": 67 | { 68 | return new NumberValue(Math.Pow(number1, number2)); 69 | } 70 | } 71 | } 72 | 73 | switch (operation) 74 | { 75 | case '+': 76 | { 77 | return new NumberValue(number1 + number2); 78 | } 79 | case '-': 80 | { 81 | return new NumberValue(number1 - number2); 82 | } 83 | case '*': 84 | { 85 | return new NumberValue(number1 * number2); 86 | } 87 | case '/': 88 | { 89 | return new NumberValue(number1 / number2); 90 | } 91 | case '%': 92 | { 93 | return new NumberValue(number1 % number2); 94 | } 95 | default: 96 | { 97 | return new NumberValue(number1 + number2); 98 | } 99 | } 100 | } 101 | 102 | override public string ToString() 103 | { 104 | string for_formatirovanie = (expr1).ToString() + " " + (operation).ToString() + " " + (expr2).ToString(); 105 | return string.Format(for_formatirovanie, expr1, operation, expr2); 106 | } 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /SourceCode/ast/BlockStatement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public class BlockStatement : Statement 10 | { 11 | private List statements; 12 | 13 | public BlockStatement() 14 | { 15 | statements = new List(); 16 | } 17 | 18 | public void add(Statement statement) 19 | { 20 | statements.Add(statement); 21 | } 22 | 23 | public void execute() 24 | { 25 | foreach (Statement statement in statements) 26 | { 27 | if (statement is AsyncFunctionStatement) 28 | { 29 | AsyncFunctionStatement fstatement = ((AsyncFunctionStatement)statement); 30 | 31 | var mytask = fstatement.async_execute(); 32 | mytask.Wait(); 33 | } 34 | else 35 | { 36 | statement.execute(); 37 | } 38 | } 39 | } 40 | 41 | public override string ToString() 42 | { 43 | StringBuilder result = new StringBuilder(); 44 | 45 | foreach (Statement statement in statements) 46 | { 47 | result.Append(statement.ToString()).Append("\n"); 48 | } 49 | return result.ToString(); 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /SourceCode/ast/BreakStatement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public class BreakStatement : Exception, Statement 10 | { 11 | public BreakStatement() 12 | { 13 | 14 | } 15 | 16 | public void execute() 17 | { 18 | throw this; 19 | } 20 | 21 | public override string ToString() 22 | { 23 | return "break"; 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /SourceCode/ast/CaseStatement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public class CaseStatement : Statement 10 | { 11 | public Statement body; 12 | public Expression main; 13 | public Expression expr; 14 | 15 | public CaseStatement(Expression expr, Expression main, Statement body) 16 | { 17 | this.body = body; 18 | this.main = main; 19 | this.expr = expr; 20 | } 21 | 22 | public void execute() 23 | { 24 | Value main1 = main.eval(); 25 | Value expr1 = expr.eval(); 26 | if (main1.type() == expr1.type()) { 27 | if (main1.asString() == expr1.asString()) 28 | { 29 | body.execute(); 30 | } 31 | } 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /SourceCode/ast/ConditionalExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public class ConditionalExpression : Expression 10 | { 11 | public enum Operator { 12 | PLUS, 13 | MINUS, 14 | MULTIPLY, 15 | DIVIDE, 16 | 17 | EQUALS, 18 | NOT_EQUALS, 19 | 20 | LT, 21 | LTEQ, 22 | GT, 23 | GTEQ, 24 | 25 | AND, 26 | OR, 27 | LOGAND, 28 | LOGOR, 29 | XOR 30 | }; 31 | private Expression expr1, expr2; 32 | private Operator operation; 33 | 34 | public ConditionalExpression(Operator operation, Expression expr1, Expression expr2) 35 | { 36 | this.operation = operation; 37 | this.expr1 = expr1; 38 | this.expr2 = expr2; 39 | } 40 | 41 | public Value eval() 42 | { 43 | Value value1 = expr1.eval(); 44 | Value value2 = expr2.eval(); 45 | 46 | double number1, number2; 47 | if (value1 is StringValue) 48 | { 49 | number1 = value1.asString().CompareTo(value2.asString()); 50 | number2 = 0; 51 | string string1 = value1.asString(); 52 | string string2 = value2.asString(); 53 | } 54 | else 55 | { 56 | number1 = value1.asDouble(); 57 | number2 = value2.asDouble(); 58 | } 59 | bool result; 60 | switch (operation) 61 | { 62 | case Operator.LT: 63 | { 64 | result = number1 < number2; break; 65 | } 66 | case Operator.LTEQ: 67 | { 68 | result = number1 <= number2; break; 69 | } 70 | case Operator.GT: 71 | { 72 | result = number1 > number2; break; 73 | } 74 | case Operator.GTEQ: 75 | { 76 | result = number1 >= number2; break; 77 | } 78 | case Operator.EQUALS: 79 | { 80 | result = number1 == number2; break; 81 | } 82 | case Operator.NOT_EQUALS: 83 | { 84 | result = number1 != number2; break; 85 | } 86 | 87 | case Operator.AND: 88 | { 89 | result = (number1 != 0) && (number2 != 0); break; 90 | } 91 | case Operator.OR: 92 | { 93 | result = (number1 != 0) || (number2 != 0); break; 94 | } 95 | case Operator.LOGOR: 96 | { 97 | result = (number1 != 0) | (number2 != 0); break; 98 | } 99 | case Operator.LOGAND: 100 | { 101 | result = (number1 != 0) & (number2 != 0); break; 102 | } 103 | case Operator.XOR: 104 | { 105 | result = (number1 != 0) ^ (number2 != 0); break; 106 | } 107 | 108 | default: 109 | { 110 | result = number1 == number2; break; 111 | } 112 | } 113 | 114 | return new NumberValue(result); 115 | } 116 | 117 | override public string ToString() 118 | { 119 | string for_formatirovanie = (expr1).ToString() + " " + (operation).ToString() + " " + (expr2).ToString(); 120 | return string.Format(for_formatirovanie, expr1, operation, expr2); 121 | } 122 | } 123 | } 124 | -------------------------------------------------------------------------------- /SourceCode/ast/ConstantExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | class ConstantExpression : Expression 10 | { 11 | private string name; 12 | 13 | public ConstantExpression(string name) 14 | { 15 | this.name = name; 16 | } 17 | 18 | public Value eval() 19 | { 20 | if (!Variables.isExists(name) && !Constants.isExists(name)) throw new Exception("Constant doesnt exists"); 21 | try 22 | { 23 | return Variables.get(name); 24 | } 25 | catch 26 | { 27 | return Constants.get(name); 28 | } 29 | } 30 | 31 | public override string ToString() 32 | { 33 | return string.Format((name).ToString(), name); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /SourceCode/ast/ContinueStatement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public class ContinueStatement : Exception, Statement 10 | { 11 | public ContinueStatement() 12 | { 13 | 14 | } 15 | 16 | public void execute() 17 | { 18 | throw this; 19 | } 20 | 21 | public override string ToString() 22 | { 23 | return "continue"; 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /SourceCode/ast/DdotEqAssignmentStatement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public class DdotEqAssignmentStatement : Statement 10 | { 11 | private string valuet; 12 | private EnumValue value; 13 | private StringValue var; 14 | private Expression value1; 15 | 16 | public DdotEqAssignmentStatement(string value, StringValue var, Expression value1) 17 | { 18 | valuet = value; 19 | this.var = var; 20 | this.value1 = value1; 21 | } 22 | 23 | public void execute() 24 | { 25 | value = new EnumValue(((EnumValue)Variables.get(valuet)).getAll()); 26 | value.set(var.asString(), value1.eval()); 27 | } 28 | 29 | override public string ToString() 30 | { 31 | return value.asString(); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /SourceCode/ast/DdotExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public class DdotExpression : Expression 10 | { 11 | //private string valuet; 12 | private string value; 13 | private string var; 14 | private EnumValue evalue; 15 | 16 | public DdotExpression(string value, string var) 17 | { 18 | this.value = value; 19 | this.var = var; 20 | } 21 | 22 | public Value eval() 23 | { 24 | evalue = (EnumValue)Variables.get(value);// new EnumValue(((EnumValue)Variables.get(value)).getAll()); 25 | return evalue.get(var); 26 | } 27 | 28 | override public string ToString() 29 | { 30 | return value; 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /SourceCode/ast/DoWhileStatement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public class DoWhileStatement : Statement 10 | { 11 | private Statement statement; 12 | private Expression condition; 13 | 14 | public DoWhileStatement(Statement statement, Expression condition) 15 | { 16 | this.statement = statement; 17 | this.condition = condition; 18 | } 19 | 20 | public void execute() 21 | { 22 | do 23 | { 24 | try 25 | { 26 | statement.execute(); 27 | } 28 | catch (BreakStatement) 29 | { 30 | break; 31 | } 32 | catch (ContinueStatement) 33 | { 34 | //continue; 35 | } 36 | } 37 | while (condition.eval().asNumber() != 0); 38 | } 39 | 40 | public override string ToString() 41 | { 42 | return "do " + statement + " while " + condition; 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /SourceCode/ast/Expression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public interface Expression 10 | { 11 | Value eval(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /SourceCode/ast/ForStatement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public class ForStatement : Statement 10 | { 11 | private Expression termination; 12 | private Statement block; 13 | private Statement initalization; 14 | private Statement increment; 15 | 16 | public ForStatement(Expression termination, Statement block, Statement initalization, Statement increment) 17 | { 18 | this.termination = termination; 19 | this.block = block; 20 | this.initalization = initalization; 21 | this.increment = increment; 22 | } 23 | 24 | public void execute() 25 | { 26 | for (initalization.execute(); termination.eval().asNumber() != 0; increment.execute()) 27 | { 28 | try 29 | { 30 | block.execute(); 31 | } 32 | catch (BreakStatement) 33 | { 34 | break; 35 | } 36 | catch (ContinueStatement) 37 | { 38 | //continue; 39 | } 40 | } 41 | } 42 | 43 | public override string ToString() 44 | { 45 | return "for " + initalization + ',' + ' ' + termination + ',' + ' ' + increment + ' ' + block; 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /SourceCode/ast/FunctionDefine.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public class FunctionDefine : Statement 10 | { 11 | private string name; 12 | private List argNames; 13 | private Statement body; 14 | 15 | public FunctionDefine(string name, List argNames, Statement body) 16 | { 17 | this.name = name; 18 | this.argNames = argNames; 19 | this.body = body; 20 | } 21 | 22 | public void execute() 23 | { 24 | Functions.set(name, new UserDefineFunction(argNames, body)); 25 | } 26 | 27 | public override string ToString() 28 | { 29 | return "def (" + argNames.ToString() + ") " + body.ToString(); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /SourceCode/ast/FunctionStatement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public class FunctionStatement : Statement 10 | { 11 | private FunctionalExpression function; 12 | public FunctionStatement(FunctionalExpression function) 13 | { 14 | this.function = function; 15 | } 16 | 17 | public void execute() 18 | { 19 | function.eval(); 20 | } 21 | 22 | public override string ToString() 23 | { 24 | return function.ToString(); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /SourceCode/ast/FunctionalExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public class FunctionalExpression : Expression 10 | { 11 | private string name; 12 | private List arguments; 13 | public string enumFunc = string.Empty; 14 | public bool isEnumFunc = false; 15 | 16 | public FunctionalExpression(string name) 17 | { 18 | this.name = name; 19 | arguments = new List(); 20 | } 21 | 22 | public FunctionalExpression(string name, List arguments) 23 | { 24 | this.name = name; 25 | this.arguments = arguments; 26 | } 27 | 28 | public void addArgument(Expression arg) 29 | { 30 | arguments.Add(arg); 31 | } 32 | 33 | public async Task async_eval() 34 | { 35 | int size = arguments.Count(); 36 | Value[] values = new Value[size]; 37 | for (int i = 0; i < size; i++) 38 | { 39 | values[i] = arguments[i].eval(); 40 | } 41 | 42 | Function function = Functions.get(name); 43 | var mytask = Task.Run(() => function.execute(values)); 44 | mytask.Wait(); 45 | } 46 | 47 | public Value enum_eval(Ldef function) 48 | { 49 | int size = arguments.Count(); 50 | Value[] values = new Value[size]; 51 | for (int i = 0; i < size; i++) 52 | { 53 | values[i] = arguments[i].eval(); 54 | } 55 | 56 | Ldef userFunction = function; 57 | if (size != userFunction.getArgsCount()) throw new Exception("Args count missmatch"); 58 | 59 | Variables.push(); 60 | 61 | for (int i = 0; i < size; i++) 62 | { 63 | Variables.set(userFunction.getArgsName(i), values[i]); 64 | } 65 | 66 | Value result = userFunction.execute(values); 67 | Variables.pop(); 68 | return result; 69 | } 70 | 71 | public void void_eval() 72 | { 73 | if (Variables.isExists(enumFunc)) 74 | { 75 | EnumValue enumValue = (EnumValue)Variables.get(enumFunc); 76 | enum_eval((Ldef)((ObjectValue)enumValue.get(name)).asObject()); 77 | return; 78 | } 79 | int size = arguments.Count(); 80 | Value[] values = new Value[size]; 81 | for (int i = 0; i < size; i++) 82 | { 83 | values[i] = arguments[i].eval(); 84 | } 85 | 86 | Function function = Functions.get(name); 87 | if (function is UserDefineFunction) 88 | { 89 | UserDefineFunction userFunction = (UserDefineFunction)function; 90 | if (size != userFunction.getArgsCount()) throw new Exception("Args count missmatch"); 91 | 92 | Variables.push(); 93 | 94 | for (int i = 0; i < size; i++) 95 | { 96 | Variables.set(userFunction.getArgsName(i), values[i]); 97 | } 98 | 99 | Value result = userFunction.execute(values); 100 | Variables.pop(); 101 | } 102 | if (function is Ldef) 103 | { 104 | Ldef userFunction = (Ldef)function; 105 | if (size != userFunction.getArgsCount()) throw new Exception("Args count missmatch"); 106 | 107 | Variables.push(); 108 | 109 | for (int i = 0; i < size; i++) 110 | { 111 | Variables.set(userFunction.getArgsName(i), values[i]); 112 | } 113 | 114 | Value result = userFunction.execute(values); 115 | Variables.pop(); 116 | } 117 | function.execute(values); 118 | } 119 | 120 | public Value eval() 121 | { 122 | if (Variables.isExists(enumFunc)) 123 | { 124 | EnumValue enumValue = (EnumValue)Variables.get(enumFunc); 125 | return enum_eval((Ldef)((ObjectValue)enumValue.get(name)).asObject()); 126 | } 127 | int size = arguments.Count(); 128 | Value[] values = new Value[size]; 129 | for (int i = 0; i < size; i++) 130 | { 131 | values[i] = arguments[i].eval(); 132 | } 133 | 134 | Function function = Functions.get(name); 135 | if (function is UserDefineFunction) 136 | { 137 | UserDefineFunction userFunction = (UserDefineFunction)function; 138 | if (size != userFunction.getArgsCount()) throw new Exception("Args count missmatch"); 139 | 140 | Variables.push(); 141 | 142 | for (int i = 0; i < size; i++) 143 | { 144 | Variables.set(userFunction.getArgsName(i), values[i]); 145 | } 146 | 147 | Value result = userFunction.execute(values); 148 | Variables.pop(); 149 | return result; 150 | } 151 | if (function is Ldef) 152 | { 153 | Ldef userFunction = (Ldef)function; 154 | if (size != userFunction.getArgsCount()) throw new Exception("Args count missmatch"); 155 | 156 | Variables.push(); 157 | 158 | for (int i = 0; i < size; i++) 159 | { 160 | Variables.set(userFunction.getArgsName(i), values[i]); 161 | } 162 | 163 | Value result = userFunction.execute(values); 164 | Variables.pop(); 165 | return result; 166 | } 167 | return function.execute(values); 168 | } 169 | 170 | public override string ToString() 171 | { 172 | return name + '(' + arguments.ToString() + ')'; 173 | } 174 | } 175 | } 176 | -------------------------------------------------------------------------------- /SourceCode/ast/IfStatement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public class IfStatement : Statement 10 | { 11 | private Expression expression; 12 | private Statement ifStatement, elseStatement; 13 | 14 | public IfStatement(Expression expression, Statement ifStatement, Statement elseStatement) 15 | { 16 | this.expression = expression; 17 | this.ifStatement = ifStatement; 18 | this.elseStatement = elseStatement; 19 | } 20 | 21 | public void execute() 22 | { 23 | double result = expression.eval().asNumber(); 24 | if (result != 0) 25 | { 26 | ifStatement.execute(); 27 | } 28 | else if (elseStatement != null) 29 | { 30 | elseStatement.execute(); 31 | } 32 | } 33 | 34 | public override string ToString() 35 | { 36 | StringBuilder result = new StringBuilder(); 37 | result.Append("if ").Append(expression).Append(' ').Append(ifStatement); 38 | if (elseStatement != null) 39 | { 40 | result.Append("\nelse ").Append(elseStatement); 41 | } 42 | return result.ToString(); 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /SourceCode/ast/LambdaExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public class LambdaExpression : Expression 10 | { 11 | public Statement statement; 12 | 13 | public LambdaExpression(Statement statement) 14 | { 15 | this.statement = statement; 16 | } 17 | 18 | public Value eval() 19 | { 20 | return new StatementValue(statement); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /SourceCode/ast/ObjectAssignmentStatement.cs: -------------------------------------------------------------------------------- 1 | using OwnLang.ast.lib; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace OwnLang.ast 9 | { 10 | class ObjectAssignmentStatement : Statement 11 | { 12 | private string variable; 13 | private Expression expression; 14 | 15 | public ObjectAssignmentStatement(string variable, Expression expression) 16 | { 17 | this.variable = variable; 18 | this.expression = expression; 19 | } 20 | 21 | public void execute() 22 | { 23 | Value result = expression.eval(); 24 | Variables.set(variable, result); 25 | } 26 | 27 | public override string ToString() 28 | { 29 | return string.Format(("object" + variable.ToString() + " = " + expression.ToString()), variable, expression); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /SourceCode/ast/PrintStatement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace OwnLang.ast.lib 9 | { 10 | public class PrintStatement : Statement 11 | { 12 | private Expression expression; 13 | 14 | public PrintStatement(Expression expression) 15 | { 16 | this.expression = expression; 17 | } 18 | 19 | public void execute() 20 | { 21 | Console.Write(expression.eval().asString()); 22 | } 23 | 24 | public override string ToString() 25 | { 26 | return "print " + expression; 27 | } 28 | } 29 | } -------------------------------------------------------------------------------- /SourceCode/ast/ReturnStatement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public class ReturnStatement : Exception, Statement 10 | { 11 | private Expression expression; 12 | private Value result; 13 | 14 | public ReturnStatement(Expression expression) 15 | { 16 | this.expression = expression; 17 | } 18 | 19 | public Value getResult() 20 | { 21 | return result; 22 | } 23 | 24 | public void execute() 25 | { 26 | result = expression.eval(); 27 | throw this; 28 | } 29 | 30 | public override string ToString() 31 | { 32 | return "return"; 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /SourceCode/ast/Statement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast 8 | { 9 | public interface Statement 10 | { 11 | void execute(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /SourceCode/ast/StatementStatement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public class StatementStatement : Statement 10 | { 11 | private Statement body; 12 | 13 | public StatementStatement(Statement body) 14 | { 15 | this.body = body; 16 | } 17 | 18 | void Statement.execute() 19 | { 20 | body.execute(); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /SourceCode/ast/SwitchStatement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public class SwitchStatement : Statement 10 | { 11 | private List cases; 12 | 13 | public SwitchStatement(List cases) 14 | { 15 | this.cases = cases; 16 | } 17 | 18 | public void execute() 19 | { 20 | foreach (CaseStatement casest in cases) { 21 | casest.execute(); 22 | } 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /SourceCode/ast/TryCatchStatement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace OwnLang.ast.lib 4 | { 5 | public class TryCatchStatement : Statement 6 | { 7 | private Statement tryStatement; 8 | private Statement catchStatement; 9 | 10 | public TryCatchStatement(Statement tryStatement, Statement catchStatement) 11 | { 12 | this.tryStatement = tryStatement; 13 | this.catchStatement = catchStatement; 14 | } 15 | 16 | public void execute() 17 | { 18 | try 19 | { 20 | tryStatement.execute(); 21 | } 22 | catch (Exception ex) 23 | { 24 | Variables.set("exception", new StringValue(ex.ToString())); 25 | catchStatement.execute(); 26 | } 27 | } 28 | 29 | public override string ToString() 30 | { 31 | return "try_catch"; 32 | } 33 | } 34 | } -------------------------------------------------------------------------------- /SourceCode/ast/UnaryExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | class UnaryExpression : Expression 10 | { 11 | private Expression expr1; 12 | private char operation; 13 | 14 | public UnaryExpression(char operation, Expression expr1) 15 | { 16 | this.operation = operation; 17 | this.expr1 = expr1; 18 | } 19 | 20 | public Value eval() 21 | { 22 | switch (operation) 23 | { 24 | case '-': 25 | { 26 | return new NumberValue(-expr1.eval().asDouble()); 27 | } 28 | case '+': 29 | { 30 | return expr1.eval(); 31 | } 32 | default: 33 | { 34 | return expr1.eval(); 35 | } 36 | } 37 | } 38 | 39 | override public string ToString() 40 | { 41 | return string.Format((operation.ToString() + expr1.ToString()).ToString(), operation, expr1); 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /SourceCode/ast/ValueExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public class ValueExpression : Expression 10 | { 11 | private Value value; 12 | 13 | public ValueExpression(Value value) 14 | { 15 | this.value = value; 16 | } 17 | 18 | public ValueExpression(double value) 19 | { 20 | this.value = new NumberValue(value); 21 | } 22 | 23 | public ValueExpression(bool value) 24 | { 25 | this.value = new BoolValue(value); 26 | } 27 | 28 | public ValueExpression(string value) 29 | { 30 | this.value = new StringValue(value); 31 | } 32 | 33 | public Value eval() 34 | { 35 | return value; 36 | } 37 | 38 | override public string ToString() 39 | { 40 | return value.asString(); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /SourceCode/ast/WhileStatement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public class WhileStatement : Statement 10 | { 11 | private Expression condition; 12 | private Statement statement; 13 | 14 | public WhileStatement(Expression condition, Statement statement) 15 | { 16 | this.condition = condition; 17 | this.statement = statement; 18 | } 19 | 20 | public void execute() 21 | { 22 | while (condition.eval().asNumber() != 0) 23 | { 24 | try 25 | { 26 | statement.execute(); 27 | } 28 | catch (BreakStatement) 29 | { 30 | break; 31 | } 32 | catch (ContinueStatement) 33 | { 34 | //continue; 35 | } 36 | } 37 | } 38 | 39 | public override string ToString() 40 | { 41 | return "while " + condition + ' ' + statement; 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /SourceCode/ast/lib/AsyncFunctionStatement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public class AsyncFunctionStatement : Statement 10 | { 11 | private FunctionalExpression function; 12 | public AsyncFunctionStatement(FunctionalExpression function) 13 | { 14 | this.function = function; 15 | } 16 | 17 | public void execute() 18 | { 19 | function.eval(); 20 | } 21 | 22 | public async Task async_execute() 23 | { 24 | var mytask = function.async_eval(); 25 | mytask.Wait(); 26 | } 27 | 28 | public override string ToString() 29 | { 30 | return function.ToString(); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /SourceCode/ast/lib/Class/Class.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public interface Class 10 | { 11 | Value execute(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /SourceCode/ast/lib/Class/ClassDefine.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public class ClassDefine : Statement 10 | { 11 | private string name; 12 | private Statement body; 13 | 14 | public ClassDefine(string name, Statement body) 15 | { 16 | this.name = name; 17 | this.body = body; 18 | } 19 | 20 | public void execute() 21 | { 22 | Classes.set(name, new UserDefineClass(body)); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /SourceCode/ast/lib/Class/ClassExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public class ClassExpression : Expression 10 | { 11 | private string name; 12 | 13 | public ClassExpression(string name) 14 | { 15 | this.name = name; 16 | } 17 | 18 | public Value eval() 19 | { 20 | Class classes = Classes.get(name); 21 | return classes.execute(); 22 | } 23 | 24 | public override string ToString() 25 | { 26 | return "class" + name + '(' + ' ' + ')'; 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /SourceCode/ast/lib/Class/ClassStatement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public class ClassStatement : Statement 10 | { 11 | private ClassExpression classes; 12 | public ClassStatement(ClassExpression classes) 13 | { 14 | this.classes = classes; 15 | } 16 | 17 | public void execute() 18 | { 19 | classes.eval(); 20 | } 21 | 22 | public override string ToString() 23 | { 24 | return classes.ToString(); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /SourceCode/ast/lib/Class/Classes.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Net; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | using System.Net.Sockets; 9 | using System.Threading; 10 | using System.Collections; 11 | using System.Diagnostics; 12 | using System.Drawing; 13 | using System.Net.Http; 14 | using System.Windows.Forms; 15 | using Microsoft.JScript; 16 | 17 | namespace OwnLang.ast.lib 18 | { 19 | public class Classes 20 | { 21 | private static Dictionary classes; 22 | 23 | static Classes() 24 | { 25 | classes = new Dictionary(); 26 | } 27 | 28 | public static bool isExists(string key) 29 | { 30 | return classes.ContainsKey(key); 31 | } 32 | 33 | public static Class get(string key) 34 | { 35 | if (!isExists(key)) throw new Exception("Unknown class " + key); 36 | return classes[key]; 37 | } 38 | 39 | public static void set(string key, Class classess) 40 | { 41 | try 42 | { 43 | classes.Add(key, classess); 44 | } 45 | catch 46 | { 47 | classes.Remove(key); 48 | classes.Add(key, classess); 49 | } 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /SourceCode/ast/lib/Class/UserDefineClass.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public class UserDefineClass : Class 10 | { 11 | private Statement body; 12 | 13 | public UserDefineClass(Statement body) 14 | { 15 | this.body = body; 16 | } 17 | 18 | public Value execute() 19 | { 20 | try 21 | { 22 | body.execute(); 23 | return NumberValue.ZERO; 24 | } 25 | catch (ReturnStatement rt) 26 | { 27 | return rt.getResult(); 28 | } 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /SourceCode/ast/lib/CommandLineArgs.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public static class CommandLineArgs 10 | { 11 | public static string[] args; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /SourceCode/ast/lib/ConstStatement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public class ConstStatement : Statement 10 | { 11 | private StringValue varname; 12 | private Expression value; 13 | 14 | public ConstStatement(StringValue varname, Expression value) 15 | { 16 | this.varname = varname; 17 | this.value = value; 18 | } 19 | 20 | public void execute() 21 | { 22 | Constants.set(varname.asString(), value.eval()); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /SourceCode/ast/lib/Constants.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Net; 5 | using System.Net.Sockets; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | 9 | namespace OwnLang.ast.lib 10 | { 11 | public class Constants 12 | { 13 | private static Dictionary constants; 14 | private static NumberValue ZERO = new NumberValue(0); 15 | 16 | static Constants() 17 | { 18 | constants = new Dictionary(); 19 | constants.Add("PI", new NumberValue(Math.PI)); 20 | constants.Add("E", new NumberValue(Math.E)); 21 | constants.Add("GOLDEN_RATIO", new NumberValue(1.618)); 22 | constants.Add("NULL", new ObjectValue(null)); 23 | constants.Add("SOCKSTREAM", new ObjectValue(SocketType.Stream)); 24 | constants.Add("TCP_PROTOCOL", new ObjectValue(ProtocolType.Tcp)); 25 | constants.Add("SOCKDGRAM", new ObjectValue(SocketType.Dgram)); 26 | constants.Add("UDP_PROTOCOL", new ObjectValue(ProtocolType.Udp)); 27 | constants.Add("ADDR_FAMILY_INTNET", new ObjectValue(AddressFamily.InterNetwork)); 28 | } 29 | 30 | public static bool isExists(string key) 31 | { 32 | return constants.ContainsKey(key); 33 | } 34 | 35 | public static Value get(string key) 36 | { 37 | if (!isExists(key)) throw new Exception("Constant doesnt exits"); 38 | return constants[key]; 39 | } 40 | 41 | public static void set(string key, Value value) 42 | { 43 | constants.Add(key, value); 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /SourceCode/ast/lib/Dictionary/DictionaryAccesExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public class DictionaryAccesExpression : Expression 10 | { 11 | private string variable; 12 | private Expression index; 13 | 14 | public DictionaryAccesExpression(string variable, Expression index) 15 | { 16 | this.variable = variable; 17 | this.index = index; 18 | } 19 | 20 | public Value eval() 21 | { 22 | Value var = Variables.get(variable); 23 | try 24 | { 25 | DictionaryValue array = (DictionaryValue)var; 26 | return array.get_by_index(index.eval().ToString()); 27 | } 28 | catch 29 | { 30 | throw new Exception("Array expected"); 31 | } 32 | } 33 | 34 | public override string ToString() 35 | { 36 | return String.Format((variable + '{' + index + '}').ToString(), variable, index); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /SourceCode/ast/lib/Dictionary/DictionaryAssignmentStatement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public class DictionaryAssignmentStatement : Statement 10 | { 11 | private string variable; 12 | private Expression index; 13 | private Expression expression; 14 | 15 | public DictionaryAssignmentStatement(string variable, Expression index, Expression expression) 16 | { 17 | this.variable = variable; 18 | this.index = index; 19 | this.expression = expression; 20 | } 21 | 22 | public void execute() 23 | { 24 | Value var = Variables.get(variable); 25 | //if (var is ArrayValue) 26 | try 27 | { 28 | DictionaryValue array = (DictionaryValue)var; 29 | //array.set((int)index.eval().asNumber(), expression.eval()); 30 | } 31 | catch 32 | { 33 | throw new Exception("Array expected"); 34 | } 35 | } 36 | 37 | public override string ToString() 38 | { 39 | return String.Format((variable + '{' + index + '}' + '=' + expression).ToString(), variable, index, expression); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /SourceCode/ast/lib/DogExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public class DogExpression : Expression 10 | { 11 | private LdefExpression ldef; 12 | public DogExpression(LdefExpression ldef) 13 | { 14 | this.ldef = ldef; 15 | } 16 | 17 | public Value eval() 18 | { 19 | Value[] zero_args = new Value[0]; 20 | return ((Ldef)((ObjectValue)ldef.eval()).asObject()).execute(zero_args); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /SourceCode/ast/lib/EqconExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public class EqconExpression : Expression 10 | { 11 | private string var; 12 | private Expression expr; 13 | 14 | public EqconExpression(string var, Expression expr) 15 | { 16 | this.var = var; 17 | this.expr = expr; 18 | } 19 | 20 | public Value eval() 21 | { 22 | Value value = expr.eval(); 23 | Variables.set(var, value); 24 | return value; 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /SourceCode/ast/lib/ForeachStatement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public class ForeachStatement : Statement 10 | { 11 | public string variable; 12 | public Expression container; 13 | public Statement block; 14 | 15 | public ForeachStatement(string variable, Expression container, Statement block) 16 | { 17 | this.variable = variable; 18 | this.container = container; 19 | this.block = block; 20 | } 21 | 22 | public void execute() 23 | { 24 | Value containerValue = container.eval(); 25 | 26 | switch (containerValue.type()) 27 | { 28 | case TokenType.TEXT: 29 | iterateString(containerValue.asString()); 30 | break; 31 | 32 | case TokenType.ARRAY: 33 | iterateArray((ArrayValue)containerValue); 34 | break; 35 | 36 | case TokenType.STACK: 37 | iterateStack((StackValue)containerValue); 38 | break; 39 | 40 | case TokenType.DICT: 41 | iterateDict((DictionaryValue)containerValue); 42 | break; 43 | 44 | default: 45 | iterateArray((ArrayValue)containerValue); 46 | break; 47 | } 48 | } 49 | 50 | private void iterateString(string str) 51 | { 52 | foreach (char ch in str.ToCharArray()) 53 | { 54 | Variables.set(variable, new StringValue(ch.ToString())); 55 | try 56 | { 57 | block.execute(); 58 | } 59 | catch (BreakStatement) 60 | { 61 | break; 62 | } 63 | catch (ContinueStatement) 64 | { 65 | // continue; 66 | } 67 | } 68 | } 69 | 70 | private void iterateArray(ArrayValue containerValue) 71 | { 72 | foreach (Value value in containerValue) 73 | { 74 | Variables.set(variable, value); 75 | try 76 | { 77 | block.execute(); 78 | } 79 | catch (BreakStatement) 80 | { 81 | break; 82 | } 83 | catch (ContinueStatement) 84 | { 85 | // continue; 86 | } 87 | } 88 | } 89 | 90 | private void iterateDict(DictionaryValue containerValue) 91 | { 92 | foreach (KeyValuePair value in containerValue) 93 | { 94 | Variables.set(variable, new StringValue(value.ToString())); 95 | try 96 | { 97 | block.execute(); 98 | } 99 | catch (BreakStatement) 100 | { 101 | break; 102 | } 103 | catch (ContinueStatement) 104 | { 105 | // continue; 106 | } 107 | } 108 | } 109 | 110 | private void iterateStack(StackValue containerValue) 111 | { 112 | foreach (Value value in containerValue) 113 | { 114 | Variables.set(variable, value); 115 | try 116 | { 117 | block.execute(); 118 | } 119 | catch (BreakStatement) 120 | { 121 | break; 122 | } 123 | catch (ContinueStatement) 124 | { 125 | // continue; 126 | } 127 | } 128 | } 129 | 130 | public override string ToString() 131 | { 132 | return "foreach "; 133 | } 134 | } 135 | } 136 | -------------------------------------------------------------------------------- /SourceCode/ast/lib/Function.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public interface Function 10 | { 11 | Value execute(Value[] args); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /SourceCode/ast/lib/IfThenElseExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public class IfThenElseExpression : Expression 10 | { 11 | private Expression boolexpr; 12 | private Expression then; 13 | private Expression elsexpr = null; 14 | 15 | public IfThenElseExpression(Expression boolexpr, Expression then, Expression elsexpr) 16 | { 17 | this.boolexpr = boolexpr; 18 | this.then = then; 19 | this.elsexpr = elsexpr; 20 | } 21 | 22 | public Value eval() 23 | { 24 | double result = boolexpr.eval().asNumber(); 25 | if (result != 0) 26 | { 27 | return then.eval(); 28 | } 29 | else if (elsexpr != null) 30 | { 31 | return elsexpr.eval(); 32 | } 33 | else 34 | { 35 | return new NumberValue(0); 36 | } 37 | } 38 | 39 | public override string ToString() 40 | { 41 | StringBuilder result = new StringBuilder(); 42 | result.Append("if ").Append(boolexpr).Append(' ').Append(then); 43 | if (elsexpr != null) 44 | { 45 | result.Append("\nelse ").Append(elsexpr); 46 | } 47 | return result.ToString(); 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /SourceCode/ast/lib/ImportStatement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace OwnLang.ast.lib 9 | { 10 | public class ImportStatement : Statement 11 | { 12 | private Expression expression; 13 | 14 | public ImportStatement(Expression expression) 15 | { 16 | this.expression = expression; 17 | } 18 | 19 | public void execute() 20 | { 21 | string input = File.ReadAllText(expression.ToString()); 22 | List tokens = new Lexer(input).tokenize(); 23 | Statement program = new Parser(tokens).parse(); 24 | program.execute(); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /SourceCode/ast/lib/InstStatement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public class InstStatement : Statement 10 | { 11 | private string enums; 12 | private string enumf; 13 | private List fdelete; 14 | 15 | public InstStatement(string enums, string enumf, List fdelete) 16 | { 17 | this.enums = enums; 18 | this.enumf = enumf; 19 | this.fdelete = fdelete; 20 | } 21 | 22 | public void execute() 23 | { 24 | Dictionary enumss = ((EnumValue)Variables.get(enums)).getAll(); 25 | foreach (string fd in fdelete) 26 | { 27 | enumss[fd] = new StringValue(fd); 28 | } 29 | Variables.set(enumf, new EnumValue(enumss)); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /SourceCode/ast/lib/Ldef.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public class Ldef : Function 10 | { 11 | private List argNames; 12 | private Statement body; 13 | 14 | public Ldef(List argNames, Statement body) 15 | { 16 | this.argNames = argNames; 17 | this.body = body; 18 | } 19 | 20 | public int getArgsCount() 21 | { 22 | return argNames.Count; 23 | } 24 | 25 | public string getArgsName(int index) 26 | { 27 | if (index < 0 || index >= getArgsCount()) return ""; 28 | return argNames[index]; 29 | } 30 | 31 | public Value execute(Value[] args) 32 | { 33 | try 34 | { 35 | body.execute(); 36 | return NumberValue.ZERO; 37 | } 38 | catch (ReturnStatement rt) 39 | { 40 | return rt.getResult(); 41 | } 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /SourceCode/ast/lib/LdefExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public class LdefExpression : Expression 10 | { 11 | private List arguments; 12 | private Statement body; 13 | 14 | public LdefExpression() 15 | { 16 | arguments = new List(); 17 | } 18 | 19 | public LdefExpression(Statement body, List arguments) 20 | { 21 | this.body = body; 22 | this.arguments = arguments; 23 | } 24 | 25 | public void addArgument(string arg) 26 | { 27 | arguments.Add(arg); 28 | } 29 | 30 | public Value eval() 31 | { 32 | return new ObjectValue(new Ldef(arguments, body)); 33 | } 34 | 35 | public override string ToString() 36 | { 37 | return "lfunc" + '(' + arguments.ToString() + ')'; 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /SourceCode/ast/lib/Marks.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public static class Marks 10 | { 11 | public static Dictionary marks = new Dictionary(); 12 | } 13 | 14 | public class EndIfException : System.Exception, Statement 15 | { 16 | public void execute() 17 | { 18 | 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /SourceCode/ast/lib/NanStatement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public class NanStatement : Statement 10 | { 11 | public void execute() 12 | { 13 | 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /SourceCode/ast/lib/NewAssignmentStatement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | class NewAssignmentStatement : Statement 10 | { 11 | private string variable; 12 | private Expression expression; 13 | private string operate; 14 | 15 | public NewAssignmentStatement(string variable, Expression expression, string operate) 16 | { 17 | this.variable = variable; 18 | this.expression = expression; 19 | this.operate = operate; 20 | } 21 | 22 | public void execute() 23 | { 24 | // expression.eval() 25 | double var = Variables.get(variable).asDouble(); 26 | Value result = new NumberValue(0); 27 | if (operate == "+") 28 | { 29 | result = new NumberValue(var += expression.eval().asDouble()); 30 | } 31 | if (operate == "-") 32 | { 33 | result = new NumberValue(var -= expression.eval().asDouble()); 34 | } 35 | if (operate == "*") 36 | { 37 | result = new NumberValue(var *= expression.eval().asDouble()); 38 | } 39 | if (operate == "/") 40 | { 41 | result = new NumberValue(var /= expression.eval().asDouble()); 42 | } 43 | Variables.set(variable, result); 44 | } 45 | 46 | public override string ToString() 47 | { 48 | return string.Format((variable.ToString() + " = " + expression.ToString()), variable, expression); 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /SourceCode/ast/lib/Stack/StackAccesExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public class StackAccesExpression : Expression 10 | { 11 | private string variable; 12 | private Expression index; 13 | 14 | public StackAccesExpression(string variable, Expression index) 15 | { 16 | this.variable = variable; 17 | this.index = index; 18 | } 19 | 20 | public Value eval() 21 | { 22 | Value var = Variables.get(variable); 23 | //if (var is ArrayValue) 24 | try 25 | { 26 | StackValue array = (StackValue)var; 27 | return array.get_by_index((int)index.eval().asNumber()); 28 | } 29 | catch 30 | { 31 | throw new Exception("Array expected"); 32 | } 33 | } 34 | 35 | public override string ToString() 36 | { 37 | return String.Format((variable + '{' + index + '}').ToString(), variable, index); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /SourceCode/ast/lib/Stack/StackAssignmentStatement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public class StackAssignmentStatement : Statement 10 | { 11 | private string variable; 12 | private Expression index; 13 | private Expression expression; 14 | 15 | public StackAssignmentStatement(string variable, Expression index, Expression expression) 16 | { 17 | this.variable = variable; 18 | this.index = index; 19 | this.expression = expression; 20 | } 21 | 22 | public void execute() 23 | { 24 | Value var = Variables.get(variable); 25 | //if (var is ArrayValue) 26 | try 27 | { 28 | StackValue array = (StackValue)var; 29 | array.set((int)index.eval().asNumber(), expression.eval()); 30 | } 31 | catch 32 | { 33 | throw new Exception("Array expected"); 34 | } 35 | } 36 | 37 | public override string ToString() 38 | { 39 | return String.Format((variable + '{' + index + '}' + '=' + expression).ToString(), variable, index, expression); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /SourceCode/ast/lib/Starcon.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public class Starcon : Expression, Statement 10 | { 11 | private int iterator; 12 | private Expression iter; 13 | private Expression expr; 14 | 15 | public Starcon(Expression iter, Expression expr) 16 | { 17 | this.iter = iter; 18 | this.expr = expr; 19 | } 20 | 21 | public Value eval() 22 | { 23 | iterator = ((NumberValue)iter.eval()).asNumber(); 24 | Value fret = new NumberValue(0); 25 | for (int i = 0; i < iterator; i++) 26 | { 27 | fret = expr.eval(); 28 | } 29 | return fret; 30 | } 31 | 32 | public void execute() 33 | { 34 | iterator = ((NumberValue)iter.eval()).asNumber(); 35 | for (int i = 0; i < iterator; i++) 36 | { 37 | expr.eval(); 38 | } 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /SourceCode/ast/lib/SuffixAssignmentStatement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | class SuffixAssignmentStatement : Statement 10 | { 11 | private string variable; 12 | private string operate; 13 | 14 | public SuffixAssignmentStatement(string variable, string operate) 15 | { 16 | this.variable = variable; 17 | this.operate = operate; 18 | } 19 | 20 | public void execute() 21 | { 22 | // expression.eval() 23 | //double number1 = 0; 24 | double var = Variables.get(variable).asDouble(); 25 | Value result = new NumberValue(0); 26 | if (operate == "+") 27 | { 28 | result = new NumberValue(var + 1); 29 | Variables.set(variable, result); 30 | } 31 | if (operate == "-") 32 | { 33 | result = new NumberValue(var - 1); 34 | Variables.set(variable, result); 35 | } 36 | Variables.set(variable, result); 37 | } 38 | 39 | public override string ToString() 40 | { 41 | return string.Format(variable.ToString() + " = ", variable); 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /SourceCode/ast/lib/SuffixExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public class SuffixExpression : Expression 10 | { 11 | private Expression expr1; 12 | private char operation; 13 | 14 | public SuffixExpression(char operation, Expression expr1) 15 | { 16 | this.operation = operation; 17 | this.expr1 = expr1; 18 | } 19 | 20 | public Value eval() 21 | { 22 | Value value1 = expr1.eval(); 23 | 24 | double number1 = value1.asDouble(); 25 | switch (operation) 26 | { 27 | case '+': 28 | { 29 | number1 += 1; 30 | Variables.set(expr1.ToString(), new NumberValue(number1)); 31 | return new NumberValue(number1); 32 | } 33 | case '-': 34 | { 35 | number1 -= 1; 36 | Variables.set(expr1.ToString(), new NumberValue(number1)); 37 | return new NumberValue(number1); 38 | } 39 | case '~': 40 | { 41 | number1 = ~(int)number1; 42 | Variables.set(expr1.ToString(), new NumberValue(number1)); 43 | return new NumberValue(number1); 44 | } 45 | default: 46 | { 47 | return new NumberValue(number1++); 48 | } 49 | } 50 | } 51 | 52 | override public string ToString() 53 | { 54 | string for_formatirovanie = (expr1).ToString() + " " + (operation).ToString() + " "; 55 | return string.Format(for_formatirovanie, expr1, operation); 56 | } 57 | } 58 | 59 | public class ForEqExpression : Expression 60 | { 61 | private Expression expr1; 62 | private Expression expr2; 63 | private char operation; 64 | 65 | public ForEqExpression(char operation, Expression expr1, Expression expr2) 66 | { 67 | this.operation = operation; 68 | this.expr1 = expr1; 69 | this.expr2 = expr2; 70 | } 71 | 72 | public Value eval() 73 | { 74 | Value value1 = expr1.eval(); 75 | Value value2 = expr2.eval(); 76 | 77 | double number1 = value1.asDouble(); 78 | double number2 = value2.asDouble(); 79 | switch (operation) 80 | { 81 | case '+': 82 | { 83 | double result; 84 | result = number1 + number2; 85 | Variables.set(expr1.ToString(), new NumberValue(result)); 86 | return new NumberValue(result); 87 | } 88 | case '-': 89 | { 90 | double result; 91 | result = number1 - number2; 92 | Variables.set(expr1.ToString(), new NumberValue(result)); 93 | return new NumberValue(result); 94 | } 95 | case '*': 96 | { 97 | double result; 98 | result = number1 * number2; 99 | Variables.set(expr1.ToString(), new NumberValue(result)); 100 | return new NumberValue(result); 101 | } 102 | case '/': 103 | { 104 | double result; 105 | result = number1 / number2; 106 | Variables.set(expr1.ToString(), new NumberValue(result)); 107 | return new NumberValue(result); 108 | } 109 | default: 110 | { 111 | return new NumberValue(number1++); 112 | } 113 | } 114 | } 115 | 116 | override public string ToString() 117 | { 118 | string for_formatirovanie = (expr1).ToString() + " " + (operation).ToString() + " "; 119 | return string.Format(for_formatirovanie, expr1, operation); 120 | } 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /SourceCode/ast/lib/UseStatement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public class UseStatement : Statement 10 | { 11 | private string expression; 12 | 13 | public UseStatement(string expression) 14 | { 15 | this.expression = expression; 16 | } 17 | 18 | public void execute() 19 | { 20 | Functions for_use = new Functions(); 21 | for_use.getModule(expression); 22 | } 23 | 24 | public override string ToString() 25 | { 26 | return "use " + expression; 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /SourceCode/ast/lib/UserDefineFunction.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public class UserDefineFunction : Function 10 | { 11 | private List argNames; 12 | private Statement body; 13 | 14 | public UserDefineFunction(List argNames, Statement body) 15 | { 16 | this.argNames = argNames; 17 | this.body = body; 18 | } 19 | 20 | public int getArgsCount() 21 | { 22 | return argNames.Count; 23 | } 24 | 25 | public string getArgsName(int index) 26 | { 27 | if (index < 0 || index >= getArgsCount()) return ""; 28 | return argNames[index]; 29 | } 30 | 31 | public Value execute(Value[] args) 32 | { 33 | try 34 | { 35 | body.execute(); 36 | return NumberValue.ZERO; 37 | } 38 | catch (ReturnStatement rt) 39 | { 40 | return rt.getResult(); 41 | } 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /SourceCode/ast/lib/UsingStatement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public class UsingStatement : Statement 10 | { 11 | private Statement block; 12 | private string variable; 13 | private Expression value; 14 | 15 | public UsingStatement(Statement block, string variable, Expression value) 16 | { 17 | this.block = block; 18 | this.variable = variable; 19 | this.value = value; 20 | } 21 | 22 | public void execute() 23 | { 24 | Variables.push(); 25 | Variables.set(variable, value.eval()); 26 | block.execute(); 27 | Variables.pop(); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /SourceCode/ast/lib/Value/ArrayValue.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace OwnLang.ast.lib 9 | { 10 | public class ArrayValue : Value, IEnumerable 11 | { 12 | private Value[] elements; 13 | 14 | public ArrayValue(int size) 15 | { 16 | this.elements = new Value[size]; 17 | } 18 | 19 | public ArrayValue(Value[] elements) 20 | { 21 | this.elements = new Value[elements.Length]; 22 | elements.CopyTo(this.elements, 0); 23 | } 24 | 25 | public ArrayValue(ArrayValue array) 26 | { 27 | new ArrayValue(array.elements); 28 | } 29 | 30 | public Value[] get() //int index 31 | { 32 | return elements; 33 | } 34 | 35 | public Value[] append(Value expression) //int index 36 | { 37 | Stack strings = new Stack(elements); 38 | strings.Push(expression); 39 | return strings.ToArray(); 40 | } 41 | 42 | public Value get_by_index(int index) 43 | { 44 | return elements[index]; 45 | } 46 | 47 | public void set(int index, Value value) 48 | { 49 | elements[index] = value; 50 | } 51 | 52 | public double asDouble() 53 | { 54 | throw new Exception("can't do it"); 55 | } 56 | 57 | public string asString() 58 | { 59 | return elements.ToString(); 60 | } 61 | 62 | public int asNumber() 63 | { 64 | throw new Exception("can't do it"); 65 | } 66 | 67 | public char asChar() 68 | { 69 | throw new Exception("can't do it"); 70 | } 71 | 72 | public object asObject() 73 | { 74 | return elements; 75 | } 76 | 77 | public IEnumerator GetEnumerator() 78 | { 79 | foreach (Value value in elements) 80 | yield return value; 81 | } 82 | 83 | public override string ToString() 84 | { 85 | return asString(); 86 | } 87 | 88 | public TokenType type() 89 | { 90 | return TokenType.ARRAY; 91 | } 92 | 93 | IEnumerator IEnumerable.GetEnumerator() 94 | { 95 | return GetEnumerator(); 96 | } 97 | 98 | IEnumerator IEnumerable.GetEnumerator() 99 | { 100 | throw new NotImplementedException(); 101 | } 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /SourceCode/ast/lib/Value/BoolValue.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public class BoolValue : Value 10 | { 11 | private bool value; 12 | public static NumberValue ZERO = new NumberValue(0); 13 | 14 | public BoolValue(bool value) 15 | { 16 | this.value = value; 17 | } 18 | 19 | public double asDouble() 20 | { 21 | if (value == true) 22 | { 23 | return 1f; 24 | } 25 | else 26 | { 27 | return 0f; 28 | } 29 | } 30 | 31 | public int asNumber() 32 | { 33 | if (value == true) 34 | { 35 | return 1; 36 | } 37 | else 38 | { 39 | return 0; 40 | } 41 | } 42 | 43 | public char asChar() 44 | { 45 | throw new Exception("can't do it"); 46 | } 47 | 48 | public bool asBool() 49 | { 50 | return value; 51 | } 52 | 53 | public string asString() 54 | { 55 | return value.ToString(); 56 | } 57 | 58 | public override string ToString() 59 | { 60 | return asString(); 61 | } 62 | 63 | public object asObject() 64 | { 65 | return value; 66 | } 67 | 68 | public TokenType type() 69 | { 70 | if (value == true) 71 | return TokenType.TRUE; 72 | else 73 | return TokenType.FALSE; 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /SourceCode/ast/lib/Value/ByteValue.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Runtime.Serialization.Formatters.Binary; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | 9 | namespace OwnLang.ast.lib 10 | { 11 | public class ByteValue : Value 12 | { 13 | private byte[] value; 14 | public static NumberValue ZERO = new NumberValue(0); 15 | 16 | public ByteValue(byte[] value) 17 | { 18 | this.value = value; 19 | } 20 | 21 | public static byte[] GetBytes(string value) 22 | { 23 | return Array.ConvertAll(value.Split('-'), s => byte.Parse(s, System.Globalization.NumberStyles.HexNumber)); 24 | } 25 | 26 | public static byte[] GetBytesFromObj(Value value) 27 | { 28 | object nvalue = value.asObject(); 29 | 30 | if (nvalue == null) 31 | return new byte[]{ 0x00 }; 32 | BinaryFormatter bf = new BinaryFormatter(); 33 | using (MemoryStream ms = new MemoryStream()) 34 | { 35 | bf.Serialize(ms, nvalue); 36 | return ms.ToArray(); 37 | } 38 | } 39 | 40 | public byte[] asByteArray() 41 | { 42 | return value; 43 | } 44 | 45 | public ArrayValue asArray() 46 | { 47 | ArrayValue values = new ArrayValue(value.Length); 48 | foreach (byte byt in value) 49 | { 50 | values.append(new ObjectValue(byt)); 51 | } 52 | return values; 53 | } 54 | 55 | public object asObject() 56 | { 57 | return value; 58 | } 59 | 60 | public double asDouble() 61 | { 62 | return BitConverter.ToDouble(value, 0); 63 | } 64 | 65 | public string asString() 66 | { 67 | return BitConverter.ToString(value); 68 | } 69 | 70 | public int asNumber() 71 | { 72 | return BitConverter.ToInt32(value, 0); 73 | } 74 | 75 | public char asChar() 76 | { 77 | return BitConverter.ToChar(value, 0); 78 | } 79 | 80 | public TokenType type() 81 | { 82 | return TokenType.BYTE; 83 | } 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /SourceCode/ast/lib/Value/DictionaryValue.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public class DictionaryValue : Value, IEnumerable> 10 | { 11 | private Dictionary elements; 12 | //private Value[] elements2; 13 | 14 | public DictionaryValue(int size) 15 | { 16 | this.elements = new Dictionary(size); 17 | } 18 | 19 | public DictionaryValue(Dictionary elements) 20 | { 21 | this.elements = new Dictionary(elements); 22 | //elements.CopyTo(this.elements2, 0); 23 | } 24 | 25 | public DictionaryValue(DictionaryValue array) 26 | { 27 | new DictionaryValue(array.elements); 28 | } 29 | 30 | public Dictionary get() //int index 31 | { 32 | //new StringValue(str.ToString()) 33 | return elements; 34 | } 35 | 36 | public object asObject() 37 | { 38 | return elements; 39 | } 40 | 41 | public Dictionary append(string key, Value expression) //int index 42 | { 43 | Dictionary strings = new Dictionary(elements); 44 | strings.Add(key, expression); 45 | return strings; 46 | } 47 | 48 | public StringValue get_by_index(string index) 49 | { 50 | return new StringValue(elements[index].ToString()); 51 | } 52 | 53 | //public void set(int index, string key, Value value) 54 | //{ 55 | // elements.ToArray()[index] = value; 56 | //} 57 | 58 | public double asDouble() 59 | { 60 | throw new Exception("can't do it"); 61 | } 62 | 63 | public string asString() 64 | { 65 | return elements.ToString(); 66 | } 67 | 68 | public int asNumber() 69 | { 70 | throw new Exception("can't do it"); 71 | } 72 | 73 | public char asChar() 74 | { 75 | throw new Exception("can't do it"); 76 | } 77 | 78 | public override string ToString() 79 | { 80 | return asString(); 81 | } 82 | 83 | public IEnumerator> GetEnumerator() 84 | { 85 | foreach (KeyValuePair value in elements) 86 | yield return value; 87 | } 88 | 89 | public TokenType type() 90 | { 91 | return TokenType.DICT; 92 | } 93 | 94 | IEnumerator> IEnumerable>.GetEnumerator() 95 | { 96 | return (IEnumerator>)GetEnumerator(); 97 | } 98 | 99 | System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() 100 | { 101 | throw new NotImplementedException(); 102 | } 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /SourceCode/ast/lib/Value/EnumValue.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public class EnumValue : Value 10 | { 11 | private Dictionary enums = new Dictionary(); 12 | 13 | public EnumValue(EnumValue enumValue) 14 | { 15 | this.enums = enumValue.getAll(); 16 | } 17 | 18 | public EnumValue(Dictionary enums) 19 | { 20 | this.enums = enums; 21 | } 22 | 23 | public Dictionary getAll() 24 | { 25 | return enums; 26 | } 27 | 28 | public Value get(string enm) 29 | { 30 | return enums[enm]; 31 | } 32 | 33 | public void set(string enm, Value value) 34 | { 35 | enums[enm] = value; 36 | } 37 | 38 | public char asChar() 39 | { 40 | return enums.ToString()[0]; 41 | } 42 | 43 | public double asDouble() 44 | { 45 | return enums.Count; 46 | } 47 | 48 | public object asObject() 49 | { 50 | return enums; 51 | } 52 | 53 | public int asNumber() 54 | { 55 | return enums.Count; 56 | } 57 | 58 | public string asString() 59 | { 60 | return enums.ToString(); 61 | } 62 | 63 | public TokenType type() 64 | { 65 | return TokenType.ENUM; 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /SourceCode/ast/lib/Value/NumberValue.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public class NumberValue : Value 10 | { 11 | private double value; 12 | public static NumberValue ZERO = new NumberValue(0); 13 | 14 | public NumberValue(bool value) 15 | { 16 | this.value = value ? 1 : 0; 17 | } 18 | 19 | public NumberValue(double value) 20 | { 21 | this.value = value; 22 | } 23 | 24 | public double asDouble() 25 | { 26 | return value; 27 | } 28 | 29 | public char asChar() 30 | { 31 | try 32 | { 33 | return char.Parse(value.ToString()); 34 | } 35 | 36 | catch 37 | { 38 | return ' '; 39 | } 40 | } 41 | 42 | public int asNumber() 43 | { 44 | return (int) value; 45 | } 46 | 47 | public string asString() 48 | { 49 | return value.ToString(); 50 | } 51 | 52 | public object asObject() 53 | { 54 | return value; 55 | } 56 | 57 | public override string ToString() 58 | { 59 | return asString(); 60 | } 61 | 62 | public TokenType type() 63 | { 64 | return TokenType.NUMBER; 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /SourceCode/ast/lib/Value/ObjectValue.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public class ObjectValue : Value 10 | { 11 | private object value; 12 | 13 | public ObjectValue(object value) 14 | { 15 | this.value = value; 16 | } 17 | 18 | public object asObject() 19 | { 20 | return value; 21 | } 22 | 23 | public char asChar() 24 | { 25 | return char.Parse(value.ToString()); 26 | } 27 | 28 | public double asDouble() 29 | { 30 | return double.Parse(value.ToString()); 31 | } 32 | 33 | public int asNumber() 34 | { 35 | return int.Parse(value.ToString()); 36 | } 37 | 38 | public string asString() 39 | { 40 | return value.ToString(); 41 | } 42 | 43 | public TokenType type() 44 | { 45 | return TokenType.OBJECT; 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /SourceCode/ast/lib/Value/StackValue.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public class StackValue : Value, IEnumerable 10 | { 11 | private Stack elements; 12 | 13 | public StackValue(int size) 14 | { 15 | this.elements = new Stack(size); 16 | } 17 | 18 | public StackValue(Stack elements) 19 | { 20 | this.elements = new Stack(elements); 21 | //elements.CopyTo(this.elements2, 0); 22 | } 23 | 24 | public object asObject() 25 | { 26 | return elements; 27 | } 28 | 29 | public StackValue(StackValue array) 30 | { 31 | new StackValue(array.elements); 32 | } 33 | 34 | public Value[] getElements() 35 | { 36 | return elements.ToArray(); 37 | } 38 | 39 | public Stack get() //int index 40 | { 41 | StringBuilder str = new StringBuilder(); 42 | str.Append("{"); 43 | for (int i = 0; i < elements.Count; i++) 44 | { 45 | str.Append(" " + new StringValue(elements.ToArray()[i].asString()) + ";"); 46 | } 47 | str.Append("}"); 48 | //new StringValue(str.ToString()) 49 | return elements; 50 | } 51 | 52 | public Stack append(Value expression) //int index 53 | { 54 | Stack strings = new Stack(elements); 55 | strings.Push(expression); 56 | return strings; 57 | } 58 | 59 | public Value get_by_index(int index) 60 | { 61 | return elements.ToArray()[index]; 62 | } 63 | 64 | public void set(int index, Value value) 65 | { 66 | elements.ToArray()[index] = value; 67 | } 68 | 69 | public double asDouble() 70 | { 71 | throw new Exception("can't do it"); 72 | } 73 | 74 | public string asString() 75 | { 76 | return elements.ToString(); 77 | } 78 | 79 | public int asNumber() 80 | { 81 | throw new Exception("can't do it"); 82 | } 83 | 84 | public char asChar() 85 | { 86 | throw new Exception("can't do it"); 87 | } 88 | 89 | public override string ToString() 90 | { 91 | return asString(); 92 | } 93 | 94 | public IEnumerator GetEnumerator() 95 | { 96 | foreach (Value value in elements) 97 | yield return value; 98 | } 99 | 100 | public TokenType type() 101 | { 102 | return TokenType.STACK; 103 | } 104 | 105 | IEnumerator IEnumerable.GetEnumerator() 106 | { 107 | return GetEnumerator(); 108 | } 109 | 110 | System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() 111 | { 112 | throw new NotImplementedException(); 113 | } 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /SourceCode/ast/lib/Value/StatementValue.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public class StatementValue : Value 10 | { 11 | private Statement value; 12 | 13 | public StatementValue(Statement value) 14 | { 15 | this.value = value; 16 | } 17 | 18 | public Statement asStatement() 19 | { 20 | return value; 21 | } 22 | 23 | public char asChar() 24 | { 25 | throw new NotImplementedException(); 26 | } 27 | 28 | public double asDouble() 29 | { 30 | throw new NotImplementedException(); 31 | } 32 | 33 | public int asNumber() 34 | { 35 | throw new NotImplementedException(); 36 | } 37 | 38 | public string asString() 39 | { 40 | throw new NotImplementedException(); 41 | } 42 | 43 | public object asObject() 44 | { 45 | return value; 46 | } 47 | 48 | public TokenType type() 49 | { 50 | return TokenType.LAMBDA; 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /SourceCode/ast/lib/Value/StringValue.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public class StringValue : Value 10 | { 11 | private string value; 12 | 13 | public StringValue(string value) 14 | { 15 | this.value = value; 16 | } 17 | 18 | public double asDouble() 19 | { 20 | try 21 | { 22 | return double.Parse(value); 23 | } 24 | 25 | catch 26 | { 27 | return 0; 28 | } 29 | } 30 | 31 | public string asString() 32 | { 33 | return value; 34 | } 35 | 36 | public int asNumber() 37 | { 38 | try 39 | { 40 | return int.Parse(value); 41 | } 42 | 43 | catch 44 | { 45 | return 0; 46 | } 47 | } 48 | 49 | public char asChar() 50 | { 51 | try 52 | { 53 | return char.Parse(value); 54 | } 55 | 56 | catch 57 | { 58 | return ' '; 59 | } 60 | } 61 | 62 | public object asObject() 63 | { 64 | return value; 65 | } 66 | 67 | public override string ToString() 68 | { 69 | return asString(); 70 | } 71 | 72 | public TokenType type() 73 | { 74 | return TokenType.TEXT; 75 | } 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /SourceCode/ast/lib/Value/Value.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public interface Value 10 | { 11 | double asDouble(); 12 | string asString(); 13 | int asNumber(); 14 | char asChar(); 15 | object asObject(); 16 | TokenType type(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /SourceCode/ast/lib/Variables.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OwnLang.ast.lib 8 | { 9 | public class Variables 10 | { 11 | private static Dictionary variables; 12 | private static ObjectValue ZERO = new ObjectValue(null); 13 | 14 | private static Stack> stack; 15 | 16 | static Variables() 17 | { 18 | stack = new Stack>(); 19 | variables = new Dictionary(); 20 | } 21 | 22 | public static void push() 23 | { 24 | stack.Push(new Dictionary(variables)); 25 | } 26 | public static void pop() 27 | { 28 | variables = stack.Pop(); 29 | } 30 | 31 | 32 | public static bool isExists(string key) 33 | { 34 | return variables.ContainsKey(key); 35 | } 36 | 37 | public static Value get(string key) 38 | { 39 | if (!isExists(key)) throw new Exception("Variable doesnt exits"); 40 | return variables[key]; 41 | } 42 | 43 | public static void set(string key, Value value) 44 | { 45 | try 46 | { 47 | variables.Add(key, value); 48 | } 49 | catch 50 | { 51 | variables.Remove(key); 52 | variables.Add(key, value); 53 | } 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /SourceCode/program/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | 5 | namespace OwnLang.ast.lib 6 | { 7 | public class Program 8 | { 9 | static void Main(string[] args) 10 | { 11 | Console.Title = "Go-Script"; 12 | CommandLineArgs.args = args; 13 | execute(args[0].ToString()); 14 | } 15 | 16 | public static void execute(string file) 17 | { 18 | string input = File.ReadAllText(file); 19 | List tokens = new Lexer(input).tokenize(); 20 | Statement program = new Parser(tokens).parse(); 21 | program.execute(); 22 | Console.WriteLine(); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /examples/class_example.gs: -------------------------------------------------------------------------------- 1 | use "std" 2 | 3 | class Test() { 4 | hello_world = "Hello, world!" 5 | 6 | testfl = ldef() { 7 | print(hello_world) 8 | } 9 | 10 | def testf() { 11 | print(hello_world) 12 | } 13 | } 14 | 15 | init Test() 16 | testfl() 17 | testf() 18 | 19 | stop() 20 | -------------------------------------------------------------------------------- /examples/client.gs: -------------------------------------------------------------------------------- 1 | use "socket" 2 | use "std" 3 | 4 | using(socket = new_socket(ADDR_FAMILY_INTNET, SOCKSTREAM, TCP_PROTOCOL)) { 5 | socket_connect(socket, socket_ip_create("127.0.0.1", 8888)) 6 | } 7 | 8 | stop() 9 | -------------------------------------------------------------------------------- /examples/command_lang.cl: -------------------------------------------------------------------------------- 1 | eq;fist;Hello, world!;print;fist;stop; 2 | -------------------------------------------------------------------------------- /examples/command_lang.gs: -------------------------------------------------------------------------------- 1 | use "std" 2 | use "files" 3 | use "winapi" 4 | 5 | variables = new_dictionary() 6 | 7 | main = ldef () { 8 | file = read(input("Enter file name: ")) 9 | clear() 10 | words = split(file, ";") 11 | 12 | for i = 0; i < length("words"); i++ { 13 | switch words[i] { 14 | case "input" { 15 | i++ 16 | add("variables", words[i], input()) 17 | } 18 | case "eq" { 19 | i += 2 20 | add("variables", words[i-1], words[i]) 21 | } 22 | case "print" { 23 | i++ 24 | print(get_by_key("variables", words[i])) 25 | } 26 | case "stop" { 27 | stop() 28 | exit() 29 | } 30 | 31 | } 32 | } 33 | } 34 | 35 | main() 36 | -------------------------------------------------------------------------------- /examples/functional.gs: -------------------------------------------------------------------------------- 1 | use "std" 2 | 3 | new = ldef (func) { 4 | print(func("Hello, world!")) 5 | } 6 | 7 | main = ldef(str) { 8 | return str 9 | } 10 | 11 | new(main) 12 | 13 | stop() 14 | -------------------------------------------------------------------------------- /examples/game.gs: -------------------------------------------------------------------------------- 1 | use "std" 2 | use "random" 3 | use "winapi" 4 | 5 | clear() 6 | 7 | player = "*" 8 | yabloko = "@" 9 | 10 | visota = 20 11 | shirina_b_sten = 20 12 | shirina_s_sten = 18 13 | 14 | player_x = 10 15 | player_y = 9 16 | yabloko_x = randint(1, 19) 17 | yabloko_y = randint(1, 17) 18 | 19 | var ochki 20 | var fps 21 | var kadrov 22 | 23 | def check_player_pos() { 24 | if (player_x == 20 || player_y == 18 || player_x == 0 || player_y == 0) { 25 | player_x = 10 26 | player_y = 9 27 | } 28 | } 29 | 30 | mainloop = ldef () { 31 | i_input = get_key() 32 | 33 | for i = 0; i <= visota; i++ { 34 | if i == 0 || i == 20 { 35 | for j = 0; j <= shirina_b_sten; j++ { 36 | sout "#" 37 | } 38 | } 39 | else { 40 | sout "#" 41 | for c = 0; c <= shirina_s_sten; c++ { 42 | sout " " 43 | } 44 | sout "#" 45 | } 46 | sout "\n" 47 | } 48 | 49 | while (true) { 50 | sleep(50) 51 | 52 | set_cursor_position(yabloko_x, yabloko_y) 53 | sout yabloko 54 | 55 | check_player_pos() 56 | 57 | set_cursor_position(player_x, player_y) 58 | sout " " + "\b" 59 | 60 | if (yabloko_x == player_x && yabloko_y == player_y) { 61 | set_cursor_position(yabloko_x, yabloko_y) 62 | sout " " 63 | ochki += 1 64 | yabloko_x = randint(1, 19) 65 | yabloko_y = randint(1, 17) 66 | } 67 | 68 | i_input = get_key() 69 | 70 | switch as_var("i_input") { 71 | case "d" { 72 | player_x += 1 73 | } 74 | case "w" { 75 | player_y -= 1 76 | } 77 | case "s" { 78 | player_y += 1 79 | } 80 | case "a" { 81 | player_x -= 1 82 | } 83 | case "x" { 84 | exit() 85 | } 86 | } 87 | 88 | check_player_pos() 89 | 90 | set_cursor_position(player_x, player_y) 91 | sout player 92 | 93 | set_cursor_position(10, 21) 94 | sout ochki 95 | } 96 | } 97 | 98 | mainloop() 99 | -------------------------------------------------------------------------------- /examples/jscalc.gs: -------------------------------------------------------------------------------- 1 | //This program needs JsEval.dll 2 | use "std" 3 | 4 | while(true) 5 | print(eval(input("Enter expression: "))) 6 | -------------------------------------------------------------------------------- /examples/newfeaturestest.gs: -------------------------------------------------------------------------------- 1 | use "std" 2 | 3 | b = 3 4 | 5 | ifthenelseexpr =if b == 3 then 6 | 1 7 | else 8 | 0 9 | 10 | 11 | *> ifthenelseexpr print("b = 3") 12 | 13 | print(ifthenelseexpr) 14 | 15 | print(byte(99)) 16 | 17 | stop() 18 | -------------------------------------------------------------------------------- /examples/object_inst.gs: -------------------------------------------------------------------------------- 1 | enum Test { 2 | string, 3 | func 4 | } 5 | Test string := "Hello, world!" 6 | Test func := ldef (father) { 7 | sout father : string 8 | } 9 | 10 | Test inst(string) New_Test 11 | New_Test string := "Hello, world!" 12 | 13 | New_Test:func(New_Test) 14 | 15 | stop() 16 | -------------------------------------------------------------------------------- /examples/preprocessor.gs: -------------------------------------------------------------------------------- 1 | use "std" 2 | 3 | #define NULLA 0 4 | 5 | #ifndef NULLA 6 | print("not defined") 7 | #endif 8 | 9 | #ifdef NULLA 10 | print(NULLA) 11 | #endif 12 | 13 | stop() 14 | -------------------------------------------------------------------------------- /examples/server.gs: -------------------------------------------------------------------------------- 1 | use "std" 2 | use "socket" 3 | 4 | ip_addr_fam = socket_ip_create("0.0.0.0", 8888) 5 | using (socket = new_socket(ADDR_FAMILY_INTNET, SOCKSTREAM, TCP_PROTOCOL)) { 6 | socket_bind(socket, ip_addr_fam) 7 | socket_listen(socket, 1000) 8 | client = socket_accept(socket) 9 | print(socket_get_socket(client)) 10 | } 11 | 12 | stop() 13 | -------------------------------------------------------------------------------- /examples/simple_calculator.gs: -------------------------------------------------------------------------------- 1 | use "std" 2 | 3 | fnum = to_int(input("Enter first number: ")) 4 | snum = to_int(input("Enter second number: ")) 5 | op = input("Enter operation (+, -, /, *): ") 6 | var result 7 | 8 | switch as_var("op") { 9 | case "+" { 10 | result = fnum + snum 11 | } 12 | 13 | case "-" { 14 | result = fnum - snum 15 | } 16 | 17 | case "*" { 18 | result = fnum * snum 19 | } 20 | 21 | case "/" { 22 | result = fnum / snum 23 | } 24 | } 25 | 26 | if (op != "+" && op != "-" && op != "*" && op != "/") { 27 | throw_new_exception("Invalid operation!") 28 | } 29 | 30 | print(result) 31 | stop() 32 | -------------------------------------------------------------------------------- /examples/socket_web_server.gs: -------------------------------------------------------------------------------- 1 | use "socket" 2 | use "std" 3 | 4 | ip = socket_ip_create("127.0.0.1", 8888) 5 | 6 | strlen = ldef (str) { 7 | var n 8 | foreach str to "i" { 9 | n++ 10 | } 11 | return n 12 | } 13 | 14 | using (sock = new_socket(ADDR_FAMILY_INTNET, SOCKSTREAM, TCP_PROTOCOL)) { 15 | socket_bind(sock, ip) 16 | socket_listen(sock, 10000000) 17 | while (true) { 18 | user = socket_accept(sock) 19 | print(socket_recieve(user)) 20 | 21 | response_body = "

Hello, world!

" 22 | 23 | response = "HTTP/1.1 200 OK\r\nVersion: HTTP/1.1\r\nContent-Type: text/html; charset=utf-8\r\nContent-Length:" + 24 | strlen(response_body) + "\r\n\r\n" + response_body 25 | 26 | socket_send(user, response) 27 | 28 | socket_close(user) 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /gs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bas1c1/Go-Script/8d1b398ca04645e845b316c12e14e633df48a824/gs.png --------------------------------------------------------------------------------