Interpreter side of tryrun is mostly done.

pull/15/head
Jussi Pakkanen 12 years ago
parent 208e0122fb
commit 427cfbf569
  1. 7
      environment.py
  2. 34
      interpreter.py
  3. 59
      test cases/common/39 tryrun/meson.build

@ -23,6 +23,13 @@ class EnvironmentException(Exception):
def __init(self, *args, **kwargs):
Exception.__init__(self, *args, **kwargs)
class RunResult():
def __init__(self, compiled, returncode=999, stdout='UNDEFINED', stderr='UNDEFINED'):
self.compiled = compiled
self.returncode = returncode
self.stdout = stdout
self.stderr = stderr
class CCompiler():
def __init__(self, exelist):
if type(exelist) == type(''):

@ -38,6 +38,28 @@ class InterpreterObject():
return self.methods[method_name](args, kwargs)
raise InvalidCode('Unknown method "%s" in object.' % method_name)
class TryRunResultHolder(InterpreterObject):
def __init__(self, res):
super().__init__()
self.res = res
self.methods.update({'returncode' : self.returncode_method,
'compiled' : self.compiled_method,
'stdout' : self.stdout_method,
'stderr' : self.stderr_method,
})
def returncode_method(self, args, kwargs):
return self.res.returncode
def compiled_method(self, args, kwargs):
return self.res.compiled
def stdout_method(self, args, kwargs):
return self.res.stdout
def stderr_method(self, args, kwargs):
return self.res.stderr
class RunProcess(InterpreterObject):
def __init__(self, command_array, curdir):
@ -627,8 +649,20 @@ class CompilerHolder(InterpreterObject):
'get_id': self.get_id_method,
'sizeof': self.sizeof_method,
'has_header': self.has_header_method,
'run' : self.run_method,
})
def run_method(self, args, kwargs):
if len(args) != 1:
raise InterpreterException('Run method takes exactly one positional argument.')
code = args[0]
if isinstance(code, nodes.StringStatement):
code = code.get_value()
if not isinstance(code, str):
raise InterpreterException('First argument is not a string.')
result = environment.RunResult(True, 0, 'stdout', 'stderr')
return TryRunResultHolder(result)
def get_id_method(self, args, kwargs):
return self.compiler.get_id()

@ -0,0 +1,59 @@
project('tryrun', 'c')
cc = meson.get_compiler('c')
ok_code = '''#include<stdio.h>
int main(int argc, char **argv) {
printf("%s\n", "stdout");
fprintf(stderr, "%s\n", "stderr");
return 0;
}
'''
error_code = '''int main(int argc, char **argv) {
return 1;
}
'''
no_compile_code = '''int main(int argc, char **argv) {
'''
ok = cc.run(ok_code)
err = cc.run(error_code)
noc = cc.run(no_compile_code)
if noc.compiled()
message('Compilation fail test failed.')
else
error('Fail detected.')
endif
if ok.compiled()
message('Compilation worked.')
else
error('Compilation did not work.')
endif
if ok.returncode() == 0
message('Return code ok.')
else
error('Return code fail')
endif
if err.returncode() == 1
message('Bad return code ok.')
else
error('Bad return code fail.')
endif
if ok.stdout().strip() == 'stdout'
message('Stdout ok.')
else
message('Bad stdout.')
endif
if ok.stderr().strip() == 'stderr'
message('Stderr ok.')
else
message('Bad stderr.')
endif
Loading…
Cancel
Save