abstract-syntax-tree

How to traverse typed abstract syntax tree in OCaml compiler

本小妞迷上赌 提交于 2019-12-23 12:33:07
问题 I'm trying to dump type information of all identifiers in an OCaml project, basically it's the same as traversing the typed abstract syntax tree(https://github.com/ocaml/ocaml/blob/trunk/typing/typedtree.mli). Since I'm new to OCaml compiler's codebase, I'm not sure whether the Compiler has provided apis so we could easily write a plugin to do the job or we have to hack the compiler code? Also how does this interact with OCamlbuild? Thanks for any hints or advices. 回答1: Assuming you have

scala properly defining a empty value for a abstract data type

你。 提交于 2019-12-23 12:06:10
问题 I have a ADT as follows: sealed trait Tree[A] case object EmptyTree extends Tree[Nothing] case class Leaf[A](value: A) extends Tree[A] case class Node[A](op: Seq[A] => A, branches: Tree[A]*) extends Tree[A] When i try to build a function to randomly create Trees i get a problem with the EmptyTree, the type system does not let go through def create(acc: Tree[A], currentDepth: Int): Tree[A] = currentDepth match { case maxDepth => Leaf(terminalSet(r.nextInt(terminalSet.length))) case 0 => { val

TypeScript -> AST -> TypeScript

余生颓废 提交于 2019-12-23 10:00:06
问题 Is there a way to parse a TypeScript file to an AST, modify the AST, and parse it then back to TypeScript as the tools Esprima + Escodegen are able to? Important is that I do NOT want to compile/transpile the TypeScript code first into JavaScript. 回答1: Yes, with Typescript 2.x you can transform ast. Here is a good blog post about it http://blog.scottlogic.com/2017/05/02/typescript-compiler-api-revisited.html. In the official typescript wiki it is not well documented yet. 来源: https:/

ast.literal_eval() support for set literals in Python 2.7?

混江龙づ霸主 提交于 2019-12-23 07:37:05
问题 In the What’s New in Python 2.7 document it says that support for set literals was back-ported from Python 3.1. However it appears that this support was not extended to the ast module's literal_eval() function, as illustrated below. Was this intentional, an oversight, or something else -- and what are the cleanest workarounds for creating a literal set from a string representation? (I assume the following works in Python 3.1+, right?) import ast a_set = {1,2,3,4,5} print a_set print ast

Code generating JUnit based on Abstract Syntax tree walk

馋奶兔 提交于 2019-12-22 12:25:08
问题 Assuming I have the following class and method: package generation; class HelloWorld { public boolean isEven(int val) { if ( (val % 2) == 0) return true; else return false; } } Assume I want to generated the following JUnit test: package generation; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import org.junit.Test; public class HelloWorldTest { @Test public void testIsEven() { HelloWorld h = new HelloWorld(); assertTrue(h.isEven(2)); assertFalse(h

Does using a free monad in F# imply a higher startup time and limited instructions?

一曲冷凌霜 提交于 2019-12-22 08:12:47
问题 I am reading this excellent article by Mark Seemann. In it, he gives a simple demonstration of using the free monad to model interactions using pure functions. I understand it enough to be able to write such a program, and I can appreciate the virtues of such an approach. There is one bit of code though, that has me wondering about the implications. let rec bind f = function | Free instruction -> instruction |> mapI (bind f) |> Free | Pure x -> f x The function is recursive. Given that this

AST of a project by Clang

混江龙づ霸主 提交于 2019-12-21 23:17:15
问题 I use the Clang python binding to extract the AST of c/c++ files. It works perfectly for a simple program I wrote. The problem is when I want to employ it for a big project like openssl. I can run clang for any single file of the project, but clang seems to miss some headers of the project, and just gives me the AST of a few functions of the file, not all of the functions. I set the include folder by -I, but still getting part of the functions. This is my code: import clang.cindex as cl cl

Converting Antlr syntax tree into useful objects

半腔热情 提交于 2019-12-21 17:52:23
问题 I'm currently pondering how best to take an AST generated using Antlr and convert it into useful objects which I can use in my program. The purpose of my grammar (apart from learning) is to create an executable (runtime interpretted) language. For example, how would I take an attribute sub-tree and have a specific Attribute class instanciated. E.g. The following code in my language: Print(message:"Hello stackoverflow") would product the following AST: My current line of thinking is that a

How to get only function declaration from clang AST?

我的未来我决定 提交于 2019-12-21 17:44:10
问题 I want AST for my C program and want represent in json format. To do so I used clang -Xclang -ast-dump=json -fSyntax-only main.c command. It gave a AST. but the AST contains typeDecl, Value declaration etc. along with function declaration . I want only a function declaration form my code in JSON form. How can achieve this? Here is an alternative clang-check -ast-dump -ast-dump-filter=main main.c but this cant give the result in JSON form. and when I execute this got some error messages along

Understanding precedence of assignment and logical operator in Ruby

▼魔方 西西 提交于 2019-12-21 12:30:11
问题 I can't understand precedence of Ruby operators in a following example: x = 1 && y = 2 Since && has higher precedence than = , my understanding is that similarly to + and * operators: 1 + 2 * 3 + 4 which is resolved as 1 + (2 * 3) + 4 it should be equal to: x = (1 && y) = 2 However, all Ruby sources (including internal syntax parser Ripper ) parse this as x = (1 && (y = 2)) Why? EDIT [08.01.2016] Let's focus on a subexpression: 1 && y = 2 According to precedence rules, we should try to parse