mirror of
https://github.com/mtshiba/pylyzer.git
synced 2025-08-04 06:18:18 +00:00
feat: support named-expr (walrus operator)
This commit is contained in:
parent
2a98535d4c
commit
deb6736f9f
2 changed files with 37 additions and 11 deletions
|
@ -10,17 +10,17 @@ use erg_common::traits::{Locational, Stream};
|
||||||
use erg_common::{fmt_vec, log, set};
|
use erg_common::{fmt_vec, log, set};
|
||||||
use erg_compiler::artifact::IncompleteArtifact;
|
use erg_compiler::artifact::IncompleteArtifact;
|
||||||
use erg_compiler::erg_parser::ast::{
|
use erg_compiler::erg_parser::ast::{
|
||||||
Accessor, Args, BinOp, Block, ClassAttr, ClassAttrs, ClassDef, ConstAccessor, ConstApp,
|
Accessor, Args, BinOp, Block, ClassAttr, ClassAttrs, ClassDef, Compound, ConstAccessor,
|
||||||
ConstArgs, ConstAttribute, ConstBinOp, ConstBlock, ConstDict, ConstExpr, ConstKeyValue,
|
ConstApp, ConstArgs, ConstAttribute, ConstBinOp, ConstBlock, ConstDict, ConstExpr,
|
||||||
ConstLambda, ConstList, ConstListWithLength, ConstNormalList, ConstNormalSet, ConstPosArg,
|
ConstKeyValue, ConstLambda, ConstList, ConstListWithLength, ConstNormalList, ConstNormalSet,
|
||||||
ConstSet, Decorator, Def, DefBody, DefId, DefaultParamSignature, Dict, Dummy, Expr, Identifier,
|
ConstPosArg, ConstSet, Decorator, Def, DefBody, DefId, DefaultParamSignature, Dict, Dummy,
|
||||||
KeyValue, KwArg, Lambda, LambdaSignature, List, ListComprehension, Literal, Methods, Module,
|
Expr, Identifier, KeyValue, KwArg, Lambda, LambdaSignature, List, ListComprehension, Literal,
|
||||||
NonDefaultParamSignature, NormalDict, NormalList, NormalRecord, NormalSet, NormalTuple,
|
Methods, Module, NonDefaultParamSignature, NormalDict, NormalList, NormalRecord, NormalSet,
|
||||||
ParamPattern, ParamTySpec, Params, PosArg, PreDeclTypeSpec, ReDef, Record, RecordAttrs, Set,
|
NormalTuple, ParamPattern, ParamTySpec, Params, PosArg, PreDeclTypeSpec, ReDef, Record,
|
||||||
SetComprehension, Signature, SubrSignature, SubrTypeSpec, Tuple, TupleTypeSpec, TypeAppArgs,
|
RecordAttrs, Set, SetComprehension, Signature, SubrSignature, SubrTypeSpec, Tuple,
|
||||||
TypeAppArgsKind, TypeAscription, TypeBoundSpec, TypeBoundSpecs, TypeSpec, TypeSpecWithOp,
|
TupleTypeSpec, TypeAppArgs, TypeAppArgsKind, TypeAscription, TypeBoundSpec, TypeBoundSpecs,
|
||||||
UnaryOp, VarName, VarPattern, VarRecordAttr, VarRecordAttrs, VarRecordPattern, VarSignature,
|
TypeSpec, TypeSpecWithOp, UnaryOp, VarName, VarPattern, VarRecordAttr, VarRecordAttrs,
|
||||||
VisModifierSpec,
|
VarRecordPattern, VarSignature, VisModifierSpec,
|
||||||
};
|
};
|
||||||
use erg_compiler::erg_parser::desugar::Desugarer;
|
use erg_compiler::erg_parser::desugar::Desugarer;
|
||||||
use erg_compiler::erg_parser::token::{Token, TokenKind, AS, COLON, DOT, EQUAL};
|
use erg_compiler::erg_parser::token::{Token, TokenKind, AS, COLON, DOT, EQUAL};
|
||||||
|
@ -1948,6 +1948,29 @@ impl ASTConverter {
|
||||||
let stringify = self.convert_ident("str".to_string(), loc);
|
let stringify = self.convert_ident("str".to_string(), loc);
|
||||||
stringify.call1(expr).into()
|
stringify.call1(expr).into()
|
||||||
}
|
}
|
||||||
|
py_ast::Expr::NamedExpr(named) => {
|
||||||
|
let loc = named.location();
|
||||||
|
let target = self.convert_expr(*named.target);
|
||||||
|
let target_pat = match &target {
|
||||||
|
Expr::Accessor(Accessor::Ident(ident)) => VarPattern::Ident(ident.clone()),
|
||||||
|
_ => {
|
||||||
|
log!(err "unimplemented: {:?}", target);
|
||||||
|
VarPattern::Ident(Identifier::private("_".into()))
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let value = self.convert_expr(*named.value);
|
||||||
|
let assign = Token::new(
|
||||||
|
TokenKind::Assign,
|
||||||
|
"=",
|
||||||
|
loc.row.get(),
|
||||||
|
loc.column.to_zero_indexed(),
|
||||||
|
);
|
||||||
|
let def = Def::new(
|
||||||
|
Signature::Var(VarSignature::new(target_pat, None)),
|
||||||
|
DefBody::new(assign, Block::new(vec![value]), DefId(0)),
|
||||||
|
);
|
||||||
|
Expr::Compound(Compound::new(vec![Expr::Def(def), target]))
|
||||||
|
}
|
||||||
_other => {
|
_other => {
|
||||||
log!(err "unimplemented: {:?}", _other);
|
log!(err "unimplemented: {:?}", _other);
|
||||||
Expr::Dummy(Dummy::new(None, vec![]))
|
Expr::Dummy(Dummy::new(None, vec![]))
|
||||||
|
|
|
@ -44,3 +44,6 @@ assert j == 2
|
||||||
with open("test.py") as f:
|
with open("test.py") as f:
|
||||||
for line in f.readlines():
|
for line in f.readlines():
|
||||||
print("line: " + line)
|
print("line: " + line)
|
||||||
|
|
||||||
|
print(x := 1)
|
||||||
|
print(x)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue