C和C++以外的哪些语言具有显式引用和指针类型限定符?人们似乎很容易被类型的从右到左阅读顺序混淆,其中char*&"是指向字符的指针",或"字符指针引用"; 使用显式引用的任何语言都可以使用从左到右的阅读顺序,例如&*char/ ref ptr char?我正在研究一个小语言项目,易读性是我关注的主要问题之一.
在我看来,这是一个对于一个人来说很容易对搜索引擎很难提供答案的问题之一.提前致谢!
我为通用编程语言编写了一个编译器,它可以生成一个优化的输入解析树.然后,通过预处理器运行此中间格式,将其转换为目标语言,以便后续编译为本机可执行文件.
目前唯一的目标语言是C++,但我也想提供其他解决方案,以防某些程序可能会受益于使用后端进行编译,后端更好地支持源语言中的某些结构.
是否有任何语言设计用于或非常适合编译器目标的角色?
我知道LLVM,虽然它本身就是一个令人兴奋的项目,但我认为它太低了,无法直接定位.我正在寻找具有高质量实现的通用,中高级语言,其语法能够由C预处理器生成 - 因此不像Python或Ruby.对lambdas的支持会很好,但并不是绝对必要的.
c++ compiler-construction programming-languages c-preprocessor
我试图在for循环的条件内分配数组内的值:
#include <iostream>
using namespace std;
int* c;
int n;
int main()
{
scanf("%d", &n);
c = new int[n];
for (int i = 0; i < n; c[i] = i++ )
{
printf("%d \n", c[i]);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是,我没有得到所需的输出,因为n = 5 , 0 1 2 3 4. 相反,如果我正在使用该指令c[i] = ++i,我将获得输出-842150451 1 2 3 4.能否请您解释一下我们的代码是否像这样,我该如何纠正?
我有一个union包含各种兼容struct成员:
enum Type { T1, T2, … };
struct S1 { enum Type type; … };
struct S2 { enum Type type; … };
…
union U {
enum Type type;
struct S1 as_s1;
struct S2 as_s2;
…
};
Run Code Online (Sandbox Code Playgroud)
type这些结构体的字段被视为不可变的,只type访问当前对应的联合的字段。据我所知,铸造union U *到struct S1 *,只要被定义type不变的坚持,而是相反也是如此?
struct S1 *s1 = malloc (sizeof (struct S1));
union U *u = (union U *)s1;
Run Code Online (Sandbox Code Playgroud)
也就是说,仅分配sizeof (struct S1)字节并将结果强制转换为安全union U * …
我觉得似乎应该有一个简单的方法来做到这一点,但是四处搜寻并没有给我带来好的线索。我只想open()通过管道连接到应用程序,向其中写入一些数据,然后将子进程的输出发送到STDOUT调用脚本的。
open(my $foo, '|-', '/path/to/foo');
print $foo 'input'; # Should behave equivalently to "print 'output'"
close($foo);
Run Code Online (Sandbox Code Playgroud)
有没有简单的方法可以做到这一点,或者我遇到了Perl众多“无法从这里到达那里”的时刻之一?
我有两个字:
AGCGCGATAGC
^^^^
TAGCTATATATA
^^^^
Run Code Online (Sandbox Code Playgroud)
第二个单词TAGC与第一个单词相同.所以我想得到的结果是:
AGCGCGATAGCTATATATA
^^^^
Run Code Online (Sandbox Code Playgroud)
是否有任何结合这些词的Perl功能?使用串联可能会导致重复:
AGCGCGATAGCTAGCTATATATA
^^^^^^^^
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用SDL_ttf创建字体字典,就像我用SDL_image制作字典一样.由于字体存储时pnt_size我创建了一个包含此信息的结构:
struct fontinfo
{
string assetname;
int size;
};
Run Code Online (Sandbox Code Playgroud)
其次是两个词典:
map<string, SDL_Surface*> imageDictionary;
map<fontinfo*, TTF_Font*> fontDictionary;
Run Code Online (Sandbox Code Playgroud)
两者之间的区别在于字体字典不仅需要包含文件的字符串,还需要包含字体的大小.
然后,当对象请求图像或字体时,它会为其调用get函数.现在getSprite工作正常:
SDL_Surface* ResourceManager::getSprite(string assetname)
{
if (assetname == "")
return NULL;
map<string, SDL_Surface*>::iterator it = imageDictionary.find(assetname);
if (it != imageDictionary.end())
return it->second;
else
{
SDL_Surface* image = Load_Image(assetname);
if (image != NULL)
imageDictionary.insert(make_pair(assetname, image));
return image;
}
}
Run Code Online (Sandbox Code Playgroud)
该getFont方法几乎完全相同,除了它使用a fontinfo而不是string:
TTF_Font* ResourceManager::getFont(string assetname, int size)
{
if (assetname == "" || size …Run Code Online (Sandbox Code Playgroud) 我试图理解无点风格的概念.我做了一个函数尝试使用添加两个值uncurry.
add = (+) . uncurry
Run Code Online (Sandbox Code Playgroud)
并且结果抱怨:
No instance for (Num ((a0, b0) -> c0))
arising from a use of `+'
Possible fix:
add an instance declaration for (Num ((a0, b0) -> c0))
In the first argument of `(.)', namely `(+)'
In the expression: (+) . uncurry
In an equation for `add': add = (+) . uncurry
Run Code Online (Sandbox Code Playgroud)
这是一个声明问题吗?我试过add :: (Int, Int) -> Int,它不起作用.
我了解到可以通过两种方式调用函数:前缀和中缀。例如,说我创建了这个函数:
example :: [Char] -> [Char] -> [Char]
example x y = x ++ " " ++ y
Run Code Online (Sandbox Code Playgroud)
我可以这样称呼它为前缀:
example "Hello" "World"
Run Code Online (Sandbox Code Playgroud)
或像这样的中缀:
"Hello" `example` "World"
Run Code Online (Sandbox Code Playgroud)
两者都会导致代表字符串的字符列表"Hello World"。
但是,我现在正在学习函数组成,并且遇到了如下定义的函数:
(.) :: (b -> c) -> (a -> b) -> a -> c
Run Code Online (Sandbox Code Playgroud)
所以,说我想乘以三来求和。我会像这样写前缀调用:
negateComposedWithMultByThree = (.) negate (*3)
Run Code Online (Sandbox Code Playgroud)
和infix调用类似:
negateComposedWithMultByThree = negate `(.)` (*3)
Run Code Online (Sandbox Code Playgroud)
但是,虽然前缀调用可以编译,但infix调用却不会,而是显示错误消息:
错误:解析输入'('
看来,为了调用compose infix,我需要省略方括号并这样称呼它:
negateComposedWithMultByThree = negate . (*3)
Run Code Online (Sandbox Code Playgroud)
谁能对此有所启示?为什么"Hello" `example` "World"同时negate `(.)` (*3)不?
另外,如果我尝试使用这样的签名来实现自己的功能:
(,) :: Int -> Int …Run Code Online (Sandbox Code Playgroud) 我编写了一个TCP套接字客户端程序,允许用户输入IP,端口和消息作为参数.
它像是:
./a.out 127.0.0.1 555 test message
Run Code Online (Sandbox Code Playgroud)
我的问题是,如何将"test"(argv[3])和"message"(argv[4])等组合成一个char *message?