Ethgoin commited on
Commit
57aca7a
verified
1 Parent(s): 71df62b

Update parser.py

Browse files
Files changed (1) hide show
  1. parser.py +50 -4
parser.py CHANGED
@@ -2,6 +2,13 @@ from typing import List, Tuple, Any
2
 
3
  Token = Tuple[str, str]
4
 
 
 
 
 
 
 
 
5
  class Parser:
6
  def __init__(self, tokens: List[Token]):
7
  self.tokens = tokens
@@ -25,10 +32,14 @@ class Parser:
25
 
26
  def instruction(self) -> Any:
27
  token = self.current()
28
- if token[0] == "IDENTIFIER":
29
- return self.assignment()
30
- elif token[0] == "IF":
31
  return self.if_statement()
 
 
 
 
 
 
32
  else:
33
  raise SyntaxError(f"Instrucci贸n no v谩lida: {token}")
34
 
@@ -41,7 +52,9 @@ class Parser:
41
 
42
  def if_statement(self):
43
  self.match("IF")
 
44
  condition = self.expression()
 
45
  self.match("THEN")
46
  self.match("OPEN_BRACE")
47
  body = []
@@ -50,9 +63,33 @@ class Parser:
50
  self.match("CLOSE_BRACE")
51
  return {"type": "if", "condition": condition, "body": body}
52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  def expression(self):
54
  left = self.term()
55
- while self.current()[0] in ("PLUS", "MINUS"):
56
  op = self.match(self.current()[0])[0]
57
  right = self.term()
58
  left = {"type": "binop", "op": op, "left": left, "right": right}
@@ -64,5 +101,14 @@ class Parser:
64
  return {"type": "var", "value": self.match("IDENTIFIER")[1]}
65
  elif token_type in ("INT", "FLOAT"):
66
  return {"type": "num", "value": self.match(token_type)[1]}
 
 
 
 
 
 
 
 
 
67
  else:
68
  raise SyntaxError(f"Expresi贸n inv谩lida: {self.current()}")
 
2
 
3
  Token = Tuple[str, str]
4
 
5
+ # Lista de funciones rob贸ticas v谩lidas
6
+ FUNCTIONS = {
7
+ "MOVE_FORWARD", "MOVE_BACKWARD", "ROTATE", "STOP", "WAIT", "TURN_LEFT",
8
+ "TURN_RIGHT", "TOGGLE_LIGHT", "CALIBRATE", "PRINT", "SET", "READ_SENSOR",
9
+ "ACTIVATE_SENSOR", "DEACTIVATE_SENSOR", "START", "REBOOT", "SHUTDOWN"
10
+ }
11
+
12
  class Parser:
13
  def __init__(self, tokens: List[Token]):
14
  self.tokens = tokens
 
32
 
33
  def instruction(self) -> Any:
34
  token = self.current()
35
+ if token[0] == "IF":
 
 
36
  return self.if_statement()
37
+ elif token[0] == "WHILE":
38
+ return self.while_statement()
39
+ elif token[0] in FUNCTIONS:
40
+ return self.function_call()
41
+ elif token[0] == "IDENTIFIER":
42
+ return self.assignment()
43
  else:
44
  raise SyntaxError(f"Instrucci贸n no v谩lida: {token}")
45
 
 
52
 
53
  def if_statement(self):
54
  self.match("IF")
55
+ self.match("OPEN_PAREN")
56
  condition = self.expression()
57
+ self.match("CLOSE_PAREN")
58
  self.match("THEN")
59
  self.match("OPEN_BRACE")
60
  body = []
 
63
  self.match("CLOSE_BRACE")
64
  return {"type": "if", "condition": condition, "body": body}
65
 
66
+ def while_statement(self):
67
+ self.match("WHILE")
68
+ self.match("OPEN_PAREN")
69
+ condition = self.expression()
70
+ self.match("CLOSE_PAREN")
71
+ self.match("THEN")
72
+ self.match("OPEN_BRACE")
73
+ body = []
74
+ while self.current()[0] != "CLOSE_BRACE":
75
+ body.append(self.instruction())
76
+ self.match("CLOSE_BRACE")
77
+ return {"type": "while", "condition": condition, "body": body}
78
+
79
+ def function_call(self):
80
+ func_name = self.current()[0]
81
+ self.match(func_name)
82
+ self.match("OPEN_PAREN")
83
+ arg = None
84
+ if self.current()[0] not in ("CLOSE_PAREN",):
85
+ arg = self.expression()
86
+ self.match("CLOSE_PAREN")
87
+ self.match("SEMICOLON")
88
+ return {"type": "function", "name": func_name, "arg": arg}
89
+
90
  def expression(self):
91
  left = self.term()
92
+ while self.current()[0] in ("PLUS", "MINUS", "EQUAL", "NOT_EQUAL", "GREATER", "LESS", "AND", "OR"):
93
  op = self.match(self.current()[0])[0]
94
  right = self.term()
95
  left = {"type": "binop", "op": op, "left": left, "right": right}
 
101
  return {"type": "var", "value": self.match("IDENTIFIER")[1]}
102
  elif token_type in ("INT", "FLOAT"):
103
  return {"type": "num", "value": self.match(token_type)[1]}
104
+ elif token_type in ("TRUE", "FALSE"):
105
+ return {"type": "bool", "value": self.match(token_type)[0]}
106
+ elif token_type == "STRING":
107
+ return {"type": "string", "value": self.match("STRING")[1]}
108
+ elif token_type == "OPEN_PAREN":
109
+ self.match("OPEN_PAREN")
110
+ expr = self.expression()
111
+ self.match("CLOSE_PAREN")
112
+ return expr
113
  else:
114
  raise SyntaxError(f"Expresi贸n inv谩lida: {self.current()}")