abstract-syntax-tree

python ast vs json for str to dict translation

穿精又带淫゛_ 提交于 2020-01-03 20:28:32
问题 I have a piece of code that receives a string formatted as a python dictionary "{'a':'1','b':'2',...}" which I need to convert to a proper dictionary. I have tried two approaches, using json.loads(s) and ast.literal_eval(s) ast seems to be much more robust, accepting any form of quotes in the string and "just works" while json seems to be very picky about the quoting specifics and wouldn't fail on only a single form of quote format. I really would like to be as flexible as possible with the

Babel: Replacing ArrowFunctionExpression vs FunctionDeclaration in ExportDefaultDeclaration

走远了吗. 提交于 2020-01-03 19:31:34
问题 Say I have the following code I want to transform: export default () => {}; The following visitor code works: export default function ({ types: t }) { return { visitor: { ArrowFunctionExpression(path) { if (!path.node.body.directives.some(d => d.value.value === 'inject')) return; path.node.body.directives = path.node.body.directives.filter(d => d.value.value !== 'inject'); path.replaceWith(t.arrayExpression([path.node])); } } }; } That results in the following output: export default [() => {}

Babel: Replacing ArrowFunctionExpression vs FunctionDeclaration in ExportDefaultDeclaration

一世执手 提交于 2020-01-03 19:31:10
问题 Say I have the following code I want to transform: export default () => {}; The following visitor code works: export default function ({ types: t }) { return { visitor: { ArrowFunctionExpression(path) { if (!path.node.body.directives.some(d => d.value.value === 'inject')) return; path.node.body.directives = path.node.body.directives.filter(d => d.value.value !== 'inject'); path.replaceWith(t.arrayExpression([path.node])); } } }; } That results in the following output: export default [() => {}

libclang: missing some statements in the AST?

☆樱花仙子☆ 提交于 2020-01-03 17:09:13
问题 I've wrote a test program (parse_ast.c) to parse a c source file (tt.c) to see how libclang works, the output is the hierarchical structure of the AST: Here is the test file: /* tt.c */ // line 1 #include <unistd.h> #include <stdio.h> typedef ssize_t (*write_fn_t)(int, const void *, size_t); void indirect_write(write_fn_t write_fn) { // line 7 (*write_fn)(1, "indirect call\n", 14); } void direct_write() { // line 11 write(1, "direct call\n", 12); // line 12 mising in the ast? } int main() { /

Expanding math expression (expanding parentheses)

隐身守侯 提交于 2020-01-02 10:42:17
问题 I have a expression like this a*(b+c) and I have successfully parsed into an AST so it finally becomes: I'm trying to expand the expression it finally becomes a*b + a*c , but with no luck. I would like to know an algorithm to expand the expression, or maybe a library to do it, preferably for .NET. 回答1: In Symja you can use the Distribute() or Expand() function for your problem: package org.matheclipse.core.examples; import org.matheclipse.core.eval.ExprEvaluator; import org.matheclipse.core

Expanding math expression (expanding parentheses)

二次信任 提交于 2020-01-02 10:42:15
问题 I have a expression like this a*(b+c) and I have successfully parsed into an AST so it finally becomes: I'm trying to expand the expression it finally becomes a*b + a*c , but with no luck. I would like to know an algorithm to expand the expression, or maybe a library to do it, preferably for .NET. 回答1: In Symja you can use the Distribute() or Expand() function for your problem: package org.matheclipse.core.examples; import org.matheclipse.core.eval.ExprEvaluator; import org.matheclipse.core

Expanding math expression (expanding parentheses)

冷暖自知 提交于 2020-01-02 10:42:09
问题 I have a expression like this a*(b+c) and I have successfully parsed into an AST so it finally becomes: I'm trying to expand the expression it finally becomes a*b + a*c , but with no luck. I would like to know an algorithm to expand the expression, or maybe a library to do it, preferably for .NET. 回答1: In Symja you can use the Distribute() or Expand() function for your problem: package org.matheclipse.core.examples; import org.matheclipse.core.eval.ExprEvaluator; import org.matheclipse.core

AST-based search for Eclipse

你说的曾经没有我的故事 提交于 2020-01-01 05:37:09
问题 Is there a plug-in for Eclipse that lets you search based on the Java AST (Abstract Syntax Tree) of your project files? The "Java Search" feature doesn't seem to cover cases like: "Get me all the fields declared as type 'X' in all classes" I can imagine many more possibilities that would open up with an AST-based search, but I don't even know if such a plug-in would have practical performance. Update : As pointed out by Kevin below, the Java Search feature does cover the use case I mentioned.

How can I find all member field read/writes using Clang?

ぃ、小莉子 提交于 2020-01-01 05:18:09
问题 Given a C++ source code, I want to find the class fields that every function writes and reads. What is the best way of doing this using the Clang frontend? (I'm not asking for a detailed explanation of all the steps; however a starting point for an efficient solution would be great.) So far I tried parsing statements using the RecursiveASTVisitor, but keeping track of node connections is difficult. Also, I cannot figure out how to keep track of something like below: int& x = m_int_field; x++;

Clang : What does AST (abstract syntax tree) look like?

点点圈 提交于 2019-12-31 22:24:14
问题 Hi I am new in Compiler development, and am wondering how AST look like. I have a small section of code, and I use Clang for generating the AST. I don't get much information out of it. From the looks of it, the Syntax tree is exactly the same as the source, except for one struct that is added to almost any sample I test with. Source: class A { public: int *a, *b, *c; int i; void sum() { a = new int[5]; b = new int[5]; c = new int[5]; for (i = 0; i < 5; i++) { a[i] = i; b[i] = i; } for (i = 0;