#ifndef INSTRUCTION_H
#define INSTRUCTION_H
#include <string>
class Rule
{
public:
Rule(const std::string& rule);
std::string Name;
std::string Out;
};
class Command
{
public:
Command(const std::string& In);
std::string Label;
std::string InstructName;
std::string u;
std::string v;
std::string Comments;
};
#endif
#include "instruction.h"
#include <string>
using namespace std;
Rule::Rule(const string& rule)
{
size_t Pos1, Pos2, Pos3;
Pos1 = rule.find('=');
Pos2 = rule.find_first_not_of(" \t",Pos1+1);
if (Pos2 != string::npos)
{
Out = rule.substr(Pos2);
}
else
{
Out.clear();
}
Pos2 = rule.find_first_not_of(" \t");
Pos3 = rule.find_first_of(" \t",Pos2+1);
if (Pos2 < Pos1)
{
Name = rule.substr(Pos2,Pos3 >= Pos1 ? Pos1-Pos2 : Pos3-Pos2);
}
else
{
Name.clear();
}
}
Command::Command(const string& In)
{
string Line;
int Pos, Pos1;
Line = In;
Pos = Line.find(';');
if (Pos != string::npos) // if there are comments
{
Comments = Line.substr(Pos);
Line.erase(Pos); // get and erase comments from string
}
else // there's no comment
{
Comments.clear();
}
v.clear();
Pos = Line.find(',');
if (Pos != string::npos) // if there's [v] parameter
{
v = Line.substr(Pos+1);
Line.erase(Pos);
}
u.clear();
InstructName.clear();
Label.clear();
Pos = 0;
if ((Line[0] != ' ') && (Line[0] != '\t')) // if there's Label
{
Pos = Line.find_first_of(" \t");
Label = Line.substr(0,Pos);
}
Pos1 = Line.find_first_not_of(" \t",Pos);
if (Pos1 != string::npos) // if there's command name
{
Pos = Line.find_first_of(" \t",Pos1);
if (Pos != string::npos)
{
InstructName = Line.substr(Pos1, Pos-Pos1); // what if Pos=npos??
}
else
{
InstructName = Line.substr(Pos1);
}
Line.erase(0,Pos);
if (!Line.empty())
{
Pos = Line.find_first_not_of(" \t");
if (Pos != string::npos)
{
u = Line.substr(Pos); // remove label and name, left is
}
}
}
}
#include "instruction.h"
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
ifstream fin, PIC_asm;
ofstream fout;
string line, out;
vector<Rule> Table;
size_t Pos;
Command* Source;
if (argc < 2)
{
cout << "usage: convert source.as" << endl << "lease provide source file name." << endl;
return(0);
}
fin.open(argv[1]);
fin.close();
if (fin.fail())
{
cout << argv[1] << " does not exist, please check again!" << endl;
return(0);
}
fin.open("rules.txt");
fin.close();
if (fin.fail())
{
cout << "rules.txt not exist" << endl;
return (0);
}
fin.open("rules.txt");
int i=0;
while (getline(fin, line))
{
cout << line << endl;
if (line.find('=') != string::npos)
{
Table.push_back(Rule(line));
}
}
fin.close();
string OutfileName(argv[1]);
Pos = OutfileName.rfind('.');
OutfileName = OutfileName.substr(0, Pos)+".dt";
fin.open(OutfileName.c_str());
fin.close();
if (!fin.fail())
{
cout << OutfileName << " exists, do you want to overwrite? ";
while (1)
{
cin >> line;
if ((line == "y") || (line == "yes") || (line == "Y") || (line == "YES"))
{
break;
}
else if ((line == "N") || (line == "n"))
{
return (0);
}
}
}
fout.open(OutfileName.c_str());
if (fout.fail())
{
cout << "Error openning " << OutfileName << " !" << endl;
fout.close();
return (0);
}
fin.open(argv[1]);
fin.seekg(0);
cout << "\nSource file :" << endl;
while (getline(fin,line))
{
cout << line << endl;
}
fin.seekg(0);
// cout << endl << endl << endl << "The source file:" << endl;
fin.close();
PIC_asm.open(argv[1]);
while (getline(PIC_asm,line))
{
// cout << line << " CONVERT TO " ;
if (!line.empty() && (line.find_first_not_of(" \t") != string::npos))
{
Source = new Command(line);
int i;
size_t Pos1;
for (i=0; i<Table.size(); i++)
{
if(Table.Name == Source->InstructName)
{
break;
}
}
if (i == Table.size())
{
out = "; " + Table[i-1].Out + " : " + Source->InstructName;
}
else
{
out = Table.Out;
}
if (!out.empty())
{
Pos1 = out.find("");
if (Pos1 != string::npos)
{
out = out.replace(Pos1,3,Source->u);
}
Pos1 = out.find("[v]");
if (Pos1 != string::npos)
{
out = out.replace(Pos1,3,Source->v);
}
}
fout << Source->Label << '\t' << out << '\t' << Source->Comments;
fout << endl;
delete Source;
}
else
{
cout << line << endl;
}
}
PIC_asm.close();
fout.close();
cout << "*** Conversion completed. File \"" << OutfileName << "\" is created." << endl;
return (0);
}
; ### PIC to EMC asm converting rules
bsf ,[v] = BS ,[v]
bcf ,[v] = BC ,[v]
movlw = MOV A,@
=
= Unknown Command
test.dt exists, do you want to overwrite? y
Source file :
bsf ads,2
bcf 5,6 ; hello
movlw 0x38
; test line
movfw 3,b
Loop: movlw 25
*** Conversion completed. File "test.dt" is created.
jian@jian-laptop:~/codes/convert$ cat test.dt
BS ads,2
BC 5,6 ; hello
MOV A,@0x38
; test line
; Unknown Command : movfw
Loop: MOV A,@25
jian@jian-laptop:~/codes/convert$
1.可以把“using namespace std;”放到instruction.h里面,这样在instrction.h里面就可以不用引用“std::"了。
原帖由 GPS 于 8-2-2010 14:22 发表
是不是把Rule和Command放在两个文件的好点?
而且,好像两个类的成员都是只读的?是不是应该把他们都搞成private然后用个 const的getter 函数?如果要写,就加个setter, 不过偷懒的时候就都是public. 呵呵,我只是 ...
原帖由 coolmate 于 8-2-2010 14:35 发表
这是我的一个小文件的定义文件,你可以看一下。#ifndef NICTA_SISM_HELP_H
#define NICTA_SISM_HELP_H
#include
namespace nicta {
namespace sism {
class help
{
public:
...
原帖由 GPS 于 8-2-2010 15:17 发表
是不是用const函数好点阿?
std::string result() const;
std::string get_classrange() const;
用get_result, get_classrange, 或者result, classrange应该更一致点.
原帖由 coolmate 于 8-2-2010 14:34 发表
类的成员变量,无特殊需要,一律要私有。
类的成员函数,只有自身调用的,私有。
类的成员的名称,一律小写,并且私有、保护的要和公开的有区别。
一个类,放在一个file里面。
尽量避免使用:
using namespace ...
原帖由 coolmate 于 8-2-2010 16:32 发表
呵呵,是很幸福。那家伙太NB,以至于很多地方不敢用他,因为他的code用起来很简单,理解起来很难,人家怕他走了,再也没有人能接过去干。所有的code全都是模板封装的。
至于规范和材料,那是严禁泄露的。呵呵 ...
另外还有一个问题,不是关于coolmate的程序的,是一般编程的问题,如果有一组没规律的数,现在想检验另外一个数是否与这组数中的一个相等,一定要用循环的办法全部比较一遍吗?有没有什么好的办法??
头文件会被很多文件include,这里std空间里的名字全进入了全局名字空间. 造成名字污染!
如果在外面把std using使std进入全局名字空间,那C++为什么还要把标准库的名字放在std?
<thinking in c++> 强烈建议不能在header文件中using namespace std; <C++必知必会>认为在header file中using namespace std"是个馊主义...基本上又退回到了原点."
我查C++ primer,但没有找到好的方法.
using std::string;
using std::map;
....
这样做了.敲字母都敲到手痛.
太幸福了。。。。羡慕
原帖由 四香油饼 于 8-2-2010 15:50 发表
我这个小破程序引来这么多高人,真高兴,大家以后要多指点阿。
这位达人,俺有几个地方不太清楚,再请教一下:
1。关于变量的命名,一直比较糊涂,看过的程序也有很多种不同的风格,能不能讲讲你们的变量命名 ...
原帖由 四香油饼 于 8-2-2010 18:50 发表
我这个小破程序引来这么多高人,真高兴,大家以后要多指点阿。
这位达人,俺有几个地方不太清楚,再请教一下:
1。关于变量的命名,一直比较糊涂,看过的程序也有很多种不同的风格,能不能讲讲你们的变量命名 ...
原帖由 coolmate 于 8-2-2010 14:35 发表
这是我的一个小文件的定义文件,你可以看一下。#ifndef NICTA_SISM_HELP_H
#define NICTA_SISM_HELP_H
#include
namespace nicta {
namespace sism {
class help
{
public:
...
原帖由 fred_au 于 9-2-2010 00:22 发表
我记得以前Windows下编程都遵循匈牙利命名法,可能是因为M$的SDK都是用的这种命名法吧。 后来皈依Linux之后,就都是小写字母加下划线了。
原帖由 薛定谔猫 于 9-2-2010 00:44 发表
这段代码挺干净的,就是这点比较奇怪:
const void set_classrange(const std::string range); // Set the class to be searched
const void set_searchitem(const std::string item); ...
原帖由 coolmate 于 9-2-2010 15:26 发表
boost其实不是一个语言,而是c++的一套库。所有,学习的话,个人感觉,没有一定之规。
也确实没有一个roadmap的东西。开始要了解的话,就从它的官方网页开始:
http://www.boost.org/doc/libs/1_42_0/more/getti ...
原帖由 GPS 于 9-2-2010 17:18 发表
多谢coolmate。
coredump是Qt高手。
我刚刚开始用Qt,现在只有点了解。它有一套signal-slot机制,甚至可以用在console上。所以整个程序都可以搞成event-driven.
接口都比较好用,文档齐全。基本是个框架的意思,什 ...
原帖由 ubuntuhk 于 8-2-2010 00:33 发表
程序写得相当工整,赞一下![]()
![]()
一点小建议:
1.可以把“using namespace std;”放到instruction.h里面,这样在instrction.h里面就可以不用引用“std::"了。
2.如果有比较的时候,可以把变量写在后面, ...
原帖由 coolmate 于 16-2-2010 15:23 发表
忘记了一个重要的东西:this
一定要记得对于类的成员,坚持使用this来标记,比如:void nicta::sism::generator_cpp :: feed(std::vector input)
{
// clear existing source
this->source_.clear(); ...
class X
{
int value;
void setValue( int value )
{
this->value = value;
}
}
原帖由 薛定谔猫 于 16-2-2010 16:16 发表
防止名字冲突,比如:
其实这只是个code style, 不是一个must have的要求,比如Java中强制要求的getter/setter, 用是有理由的,但是如果真的不需要,非要用就是迂腐的。这点C++社区从来都是反对的。
欢迎光临 FreeOZ论坛 (https://www.freeoz.org/ibbs/) | Powered by Discuz! X3.2 |