adacore

在Ada中使用宏

Ada语言自身并不支持宏。但是强大的GCC却同样的可以为Ada语言提供宏支持。我们可以使用GCC预处理程序CPP来先进行对包含宏的源文件进行处理,输出到文件,然后将该输出文件读取并除去以#开头的行即获得了标准的Ada代码。

CPP命令:

CPP SOURCE_FILE RECORD_FILE

着需要自己写一个处理程序来去除CPP输出文件中的#开头的行

--文件读取函数:

type LPSTR is access all string;

-- Load a file by file-name into memory,return pointer

function Load(fn:in string) return LPSTR is

use ada.streams;

use ada.streams.stream_io;

type LPSEA is access all STREAM_ELEMENT_ARRAY;

PRAGMA WARNINGS(OFF);

function UC is new ada.unchecked_conversion(LPSEA,LPSTR);

PRAGMA WARNINGS(ON);

FILE:FILE_TYPE;

SIZE:COUNT;

DATA:LPSEA:=NULL;

begin

open(FILE,IN_FILE,fn);

SIZE:=STREAM_IO.SIZE(FILE);

DATA:=NEW STREAM_ELEMENT_ARRAY(1..STREAM_ELEMENT_OFFSET(SIZE));

READ(FILE,DATA.ALL,STREAM_ELEMENT_OFFSET(SIZE));

CLOSE(FILE);

RETURN UC(DATA);

exception when NAME_ERROR=>

GNAT.IO.PUT_LINE("Error : Can not find the file: "&fn);

return null;

end Load;

_____________________________________________________________

--去除#行函数:函数将指定文件名文件读出,除去#行并输出到指定文件名。

-- with ada.streams.stream_io; use ada.streams.stream_io;

-- with ada.unchecked_deallocation;

procedure clear(input_file:in string;output_file:in string) is

src:lpstr:=load(input_file);

procedure Free is new ada.unchecked_deallocation(string,LPSTR);

file:ada.streams.stream_io.file_type;

stream:ada.streams.stream_io.stream_access;

i:integer:=0;

begin

if src/=null then

loop

i:=i+1;

if src(i)='#' then

loop

src(i):=' ';

i:=i+1;

exit when i>src'last or src(i)=ASCII.LF;

end loop;

end if;

exit when i>=src'last;

end loop;

create(file,out_file,output_file);

stream:=ada.streams.stream_io.stream(file);

string'write(stream,SRC.all);

close(file);

Free(SRC);

end if;

end clear;

在经过除去#行获得最终处理过的文件后即可交由GNAT编译了。

评论

热度(1)