修复 AugAssign 操作解析错误的问题

pull/1/head
韩天峰 2 years ago
parent 4cc76cc634
commit 15c2d20b3d
  1. 47
      cases/pygame.py
  2. 47
      src/Python/Translator.php

@ -0,0 +1,47 @@
# Example file showing a circle moving on screen
from lib2to3.fixer_util import is_tuple
import pygame
# pygame setup
pygame.init()
screen = pygame.display.set_mode((1280, 720))
clock = pygame.time.Clock()
running = True
dt = 0
player_pos = pygame.Vector2(screen.get_width() / 2, screen.get_height() / 2)
while running:
# poll for events
# pygame.QUIT event means the user clicked X to close your window
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# fill the screen with a color to wipe away anything from last frame
screen.fill("purple")
pygame.draw.circle(screen, "red", player_pos, 40)
keys = pygame.key.get_pressed()
if keys[pygame.K_w]:
player_pos.y -= 300 * dt
if keys[pygame.K_s]:
player_pos.y += 300 * dt
if keys[pygame.K_a]:
player_pos.x -= 300 * dt
if keys[pygame.K_d]:
player_pos.x += 300 * dt
# flip() the display to put your work on screen
pygame.display.flip()
# limits FPS to 60
# dt is delta time in seconds since last frame, used for framerate-
# independent physics.
dt = clock.tick(60) / 1000
pygame.quit()

@ -528,9 +528,7 @@ class Translator extends \PhpAot\Core\Translator
}
break;
case 'AugAssign':
$target = $this->parseTarget($node->target);
$value = $this->parseValue($node->value);
$line = "$target += $value;";
$line = $this->parseAugAssign($node);
break;
case 'Import':
$line = $this->parseImport($node);
@ -656,24 +654,47 @@ class Translator extends \PhpAot\Core\Translator
return $code;
}
private function parseBinOp($value)
private function parseOp($op)
{
$op = $value->op->_type;
$left = $this->parseTarget($value->left);
$right = $this->parseValue($value->right);
switch ($op) {
case 'Mod':
return $left . ' % ' . $right;
return '%';
case 'Add':
return $left . ' + ' . $right;
return '+';
case 'Sub':
return $left . ' - ' . $right;
return '-';
case 'Mult':
return $left . ' * ' . $right;
return '*';
case 'Div':
return $left . ' / ' . $right;
return '/';
default:
return '$operator->' . strtolower($op) . '(' . $left . ' , ' . $right . ')';
return false;
}
}
private function parseAugAssign($value): string
{
$op = $value->op->_type;
$left = $this->parseTarget($value->target);
$right = $this->parseValue($value->value);
$op_str = $this->parseOp($op);
if ($op_str) {
return $left . ' ' . $op_str . '= ' . $right;
} else {
return $left .' = $operator->' . strtolower($op) . '(' . $left . ' , ' . $right . ')';
}
}
private function parseBinOp($value)
{
$op = $value->op->_type;
$left = $this->parseTarget($value->left);
$right = $this->parseValue($value->right);
$op_str = $this->parseOp($op);
if ($op_str) {
return $left . ' ' . $op_str . ' ' . $right;
} else {
return '$operator->' . strtolower($op) . '(' . $left . ' , ' . $right . ')';
}
}

Loading…
Cancel
Save