Jump to page: 1 2
Thread overview
vector crash
Jan 18
zjh
Jan 18
zjh
Jan 18
zjh
Jan 18
zjh
Jan 18
zjh
Jan 18
zjh
Jan 18
Renato
Jan 19
zjh
Jan 19
zjh
Jan 21
zjh
Jan 21
atzensepp
Jan 22
zjh
January 18
import dparse.ast;
import dparse.lexer;
import dparse.parser : parseModule;
import dparse.rollback_allocator : RollbackAllocator;
import core.stdcpp.vector;
import core.stdcpp.string;

Vector!string d=[];toV(a,d);
print(d);//3
foreach(e;d){
    string b=d[i];
    string m=readText(b);ff(m,b);
}

void ff(string B,string s)
{
    LexerConfig config;
    auto cache = StringCache(StringCache.defaultBucketCount);
    auto tokens = getTokensForParser(B, config, &cache);
    RollbackAllocator rba;
    auto m = parseModule(tokens,s, &rba);
    auto v= new V();
    v.visit(m);v.out(s);
}

Why can b still affect e here? Isn't b copied?
here foreach crashes.

January 18

On Thursday, 18 January 2024 at 03:07:13 UTC, zjh wrote:

>

Why can b still affect e here? Isn't b copied?
here foreach crashes.

Starting from the third one, it crashes. It's clearly vector.d, but the result is+Object.

string b=d[i];//the 3th:vector.d
//`+Object`,crashes!
January 18

On Thursday, 18 January 2024 at 03:11:57 UTC, zjh wrote:

>
string b=d[i];//the 3th:vector.d
//`+Object`,crashes!
//d[i]==>e
string b=e;

in the foreach.

January 18

On Thursday, 18 January 2024 at 03:07:13 UTC, zjh wrote:

>
import dparse.ast;
import dparse.lexer;
import dparse.parser : parseModule;
import dparse.rollback_allocator : RollbackAllocator;
import core.stdcpp.vector;
import core.stdcpp.string;
...

I have no experience with using cpp from D, and I'm not sure exactly what you are trying to do (your code is not complete), but using string in this context does not mean C++ std::string, it's a D string (immutable(char)[]).

Are you sure this is what you are wanting to do?

-Steve

January 18

On Thursday, 18 January 2024 at 03:53:09 UTC, Steven Schveighoffer wrote:

>

Are you sure this is what you are wanting to do?

-Steve

void gg(string a){
    Vector!string d=[];toV(a,d);//File to Vector
    print(d);//3,OK
    foreach(e;d){
        string b=e;//.1
        string m=readText(b);ff(m,b);
    }
}

gg: I want to traverse the 'a' file and then call the ff function .

But there was an error with the vector here, the file had 3 or 4 lines, but now it crashes on the 3rd line, and when calling print seperately, the output is normal.

I think that here b copied e, even if the ff function goes wrong, it won't affect e. I really don't know why it crashed at .1?.

January 18

On Thursday, 18 January 2024 at 04:20:12 UTC, zjh wrote:

>
foreach(e;d){
    foreach(e;d){//.0
        string b=e;
        string m=readText(b);ff(m,b);
    }

or here .0? why crash?

January 18

On Thursday, 18 January 2024 at 04:24:18 UTC, zjh wrote:

class V : ASTVisitor
{
    Vector!string f=[];
    alias visit = ASTVisitor.visit;
    override void visit(const FunctionDeclaration decl)
    {
        writeln(' '.repeat(indentLevel * 4), decl.name.text);
        f.push_back(decl.name.text);
        indentLevel++;
        scope (exit) indentLevel--;
        decl.accept(this);
    }
    void out(string s){
        string b=s~".t";toF(f,b);
    }
}

January 18

On Thursday, 18 January 2024 at 04:31:52 UTC, zjh wrote:

>

On Thursday, 18 January 2024 at 04:24:18 UTC, zjh wrote:

class V : ASTVisitor
{
    Vector!string f=[];
    alias visit = ASTVisitor.visit;
    override void visit(const FunctionDeclaration decl)
    {
        writeln(' '.repeat(indentLevel * 4), decl.name.text);
        f.push_back(decl.name.text);
        indentLevel++;
        scope (exit) indentLevel--;
        decl.accept(this);
    }
    void out(string s){
        string b=s~".t";toF(f,b);
    }
}

I don't want to sound like a StackOverflow moderator, but if you just post some random code and say something like "why crash", you won't get any helpful responses here or anywhere. The code snippets you've posted don't make any sense and as others mentioned, is obviously incomplete and won't compile.

Please explain what you're trying to do, post short code snippets that are readable (don't use names like a and e, we don't have the context at all of what you're doing - we cannot guess with names like that without spending lots of time basically de-obfuscating your code) and explain what you expected, and how the outcome differs from that.

If you do that, lots of people will help, I can guarantee!

January 19

On Thursday, 18 January 2024 at 03:07:13 UTC, zjh wrote:

import std.stdio, std.range,std.file;
import std.string:strip;

import dparse.ast;
import dparse.lexer;
import dparse.parser : parseModule;
import dparse.rollback_allocator : RollbackAllocator;
import core.stdcpp.vector;
import core.stdcpp.string;

void push(ref vector!string l,string i){
    l.push_back(i);
}
string Rff(string a){
    return readText(a);
}

void Wf(string a,string b){
    std.file.write(a,b);
}

void toF(ref vector!string b,string a){
    writeln("toV");writeln(b.length);
    string d;
    foreach(ref string e;b)d~=e~"\n";
    Wf(a,d);
}

void Qwk(ref vector!string b){
    while(1){
        auto t=b.back();if(t.length!=0)break;
        b.pop_back();
    }
}

void Pr(ref vector!string b){
    foreach(ref string a;b)writeln(a);
}//
void toV(string a,ref vector!string b){
    auto f=File(a,"r");
    while(!f.eof()){
        string l=strip(f.readln());push(b,l);
    }
    Qwk(b);
}//
string du(char c,int i){
    char[]n;n.length=i;n[]=c;return n.idup;
}

string Jjc(int l,string b){
    string a=du('+',l);return a~b;
}

class V : ASTVisitor
{
    alias visit = ASTVisitor.visit;
    vector!string f=[];
    int l;
    override void visit(const FunctionDeclaration decl)
    {
        writeln(' '.repeat(l * 4), decl.name.text);
        f.push_back(decl.name.text);
        l++;
        scope (exit) l--;
        decl.accept(this);
    }
    void _out(string s){
        string b=s~".t";toF(f,b);
    }
}

void ff(string B,string s)
{
    LexerConfig config;
    auto cache = StringCache(StringCache.defaultBucketCount);
    auto tokens = getTokensForParser(B, config, &cache);
    RollbackAllocator rba;
    auto m = parseModule(tokens,s, &rba);
    auto v= new V();
    v.visit(m);v._out(s);
}
void gg(string a){
    vector!string d=[];toV(a,d);
    Pr(d);//3,OK
    foreach(e;d){
        string b=e;//.1
        string m=readText(b);ff(m,b);
    }
}


void main(){
    string a="filename.txt";
    gg(a);
}

A fully compiled version is too tiring, mainly due to the need to change the Chinese function name.
Some names are simply changed, otherwise it would be too tiring.

January 19

On Friday, 19 January 2024 at 02:13:08 UTC, zjh wrote:

...

Compile command:

dmd -i cy2.d dparse.lib

need libdparse lib.

« First   ‹ Prev
1 2